Skip to content

v2.0.0

Choose a tag to compare

@tatethurston tatethurston released this 09 Apr 18:40
· 49 commits to main since this release
cfb9dfe

What's Changed

  • Route type generation can now also be generated outside of next build / next dev by using npx nextjs-routes.
  • router.query types must now be narrowed using router.isReady. This ensures types are correct for pages that use Automatic Static Optimization.

Next's documentation notes the following:

During prerendering, the router's query object will be empty since we do not have query information to provide during this phase. After hydration, Next.js will trigger an update to your application to provide the route parameters in the query object.

See #117 for more context and discussion.

import { useRouter } from "next/router";

const router = useRouter<"/foos/[foo]">();
// query is now typed as `{ foo?: string | undefined }`. Previously this was `{ foo: string }` which was not safe for static optimized pages
router.query;

Updating useRouter call sites to check isReady will narrow the query type:

import { useRouter } from "next/router";

const router = useRouter<"/foos/[foo]">();
// query is now typed as `{ foo?: string | undefined }`. Previously this was `{ foo: string }` which was not safe for static optimized pages
router.query;

+ if (router.isReady) {
+  // query is typed as `{ foo: string }`
+  router.query;
+ }

Full Changelog: v1.0.9...v2.0.0