Skip to content

About

Shared Next.js utility functions for ASL19 projects

Resources

Stars

0 stars

Watchers

3 watching

Forks

Repository files navigation

@asl-19/next-utils

Shared Next.js utility functions for ASL19 projects.

Install

npm install @asl-19/next-utils

Documentation

See docs/README.md for API documentation.

Testing code that uses these hooks

The hooks import next/router.js. In a project’s Vitest tests, vi.mock("next/router") (or "next/router.js") doesn’t apply to them, since Vitest loads @asl-19/next-utils from node_modules without transforming it. Either:

  • Provide the router through Next’s RouterContext, as the page would get it at runtime:

    import { RouterContext } from "next/dist/shared/lib/router-context.shared-runtime";
    
    render(
      <RouterContext.Provider value={mockRouter}>
        <IndexPage />
      </RouterContext.Provider>,
    );
  • Or have Vitest transform the package, so module mocks apply to it too:

    // vitest.config.ts
    export default defineConfig({
      test: {
        server: { deps: { inline: ["@asl-19/next-utils"] } },
      },
    });

useIndexPageLoadingAndQueryLogic only reads the router’s asPath, basePath, defaultLocale, locale and query (and marks the current history.state entry shallow), and useFocusH1OnRouteChangeComplete reads asPath and subscribes to events, so a mock router only needs those.

Development

This package uses mise to manage the Node.js and pinact versions (see mise.toml). With mise activated in your shell:

# Install Node.js and pinact
mise install

mise.toml only lists tools, so mise trusts it without mise trust. Keep it that way: adding [env], [hooks] or [tasks] would let it run code when you cd into the folder.

.npmrc and package.json (devEngines, allowScripts) configure npm supply-chain gates: a one-day release-age window, approved install scripts only, and registry-only dependencies. See AGENTS.md for details.

In this folder:

# Run tests in watch mode
npm test

# Build into dist
npm run build

# Install dependencies if needed, then type check, lint, check formatting,
# run tests, and lint package
npm run dev-lint-test

If you add a function or hook, or change the documentation comments or signature of an existing one, regenerate the API documentation (CI will fail if docs is out of date):

# Update docs/* (Markdown documentation generated by TypeDoc)
npm run docs

To check for updates to Node.js (in mise.toml), npm packages, and GitHub Actions (pinned to commit SHAs in .github/workflows):

npm run outdated

# Update GitHub Actions to the latest releases that are at least 7 days old
npm run pinact-update

To test changes in a project that uses next-utils, use yalc:

# In this folder: build, check, and publish to the local yalc store
npx yalc publish

# In the project: replace the project’s copy of next-utils with the local version
npx yalc add @asl-19/next-utils

After making further changes, run npx yalc push in this folder to update the projects using it.

Once you’re finished testing run npx yalc remove @asl-19/next-utils in the project, then run npm install to revert to the published copy of the package.

About

Shared Next.js utility functions for ASL19 projects

Resources

Stars

0 stars

Watchers

3 watching

Forks

Releases

Packages

Used by

Contributors

Languages