- CLI invocation now reads next.config.js or next.config.mjs.
- Fix
route
's handling of query keys whose value isundefined
. Fixes #206. Thanks @sleepdotexe!
- Bug fix:
usePathname
anduseParams
were incorrectly resolving toany
return types.
-
Adds support for Next.js's
app
directory.Link
accepts either static routes (no url parameters) or aRouteLiteral
string, which can be generated by theroute
helper from this library:import { route } from "nextjs-routes"; <Link href={route({ pathname: "/foos/[foo]", query: { foo: "bar" }, })} > Baz </Link>;
-
Add
RouteLiteral
type. This type represents a string that has been confirmed to be a validated application route and can be passed toLink
oruseRouter
. This is a TypeScript branded type.import { RouteLiteral } from "nextjs-routes";
route
returns aRouteLiteral
. If you construct a route string you can cast it to aRouteLiteral
so thatLink
anduseRouter
will accept it:const myRoute = `/foos/${foo}` as RouteLiteral
In general, prefer using the
route
helper to generate routes. -
Refine types for
usePathname
,useRouter
anduseParams
from"next/navigation"
to usenextjs-routes
generated types. -
Fix generated routes when using parallel-routes and intercepting-routes.
-
Fix
ref
type forLink
. Previouslyref
was missing, now it's correctly typed.
- Fix route generation on Windows. See #187. Thanks @AkanoCA!
- Add
trailingSlash
option toroute
. See #168 - Fix the generated optional catch all route type. See #183
- Sort the generated route types lexicographically.
- Add experimental support for app directory route handler. See #178.
-
Fix
GetServerSidePropsContext
andGetServerSidePropsResult
types. Thanks @po4tion! -
Change generated file location from
nextjs-routes.d.ts
to@types/nextjs-routes.d.ts
. Thanks @po4tion!To preserve the old location, make the following change to your
next.config.js
:const nextRoutes = require("nextjs-routes/config"); const withRoutes = nextRoutes({ + outDir: "", });
Otherwise, delete your old
nextjs-routes.d.ts
once@types/nextjs-routes.d.ts
is generated.
router.query
types must now be narrowed usingrouter.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.
- Route type generation can now also be generated outside of
next build
/next dev
by usingnpx nextjs-routes
.
- Enable
Link
androuter
methods to only update the routehash
. Thanks @sitch! - Publish commonjs for
route
runtime so jest tests don't require transpilation for users. Thanks @panudetjt! - Add
GetServerSidePropsContext
type. Thanks @slhck!
- Fix type definition import path for
nextjs-routes/config
so it is visible totsc
.
- Remove package.json version import. This resolves
TypeError[ERR_IMPORT_ASSERTION_TYPE_MISSING]
. See #115 for more context.
- Fix bad publish of 1.0.5
- The version of
nextjs-routes
is now included in the generatednextjs-routes.d.ts
file. - Switch
Link
to use TypeScript unions instead of function overloading. Function overloading resulted in errors that were difficult for users to understand, and created issues for some use cases.
-
LinkProps
now accept path strings for static routes:import { LinkProps } from "next/link"; // previously this would error const props: LinkProps = { href: "/foo" };
-
The
Route
type now includeshash
. This enables the following:// this will link to /foo#bar <Link href={{ pathname: "/foo", hash: "bar" }}>Foo</Link>
-
Link
now accepts anchor props:<Link href="/dashboard" className="border-indigo-500"> Dashboard </Link>
-
Update
NextRouter
type to keepquery
andpathname
bound in a union. This allows you to userouter
fromuseRouter
as an argument torouter.push
orrouter.replace
:const router = useRouter(); // reload the current page, preserving search parameters router.push(router, undefined, { locale: "fr" });
-
This library will now follow semantic versioning.
-
The previously deprecated direct invocation of nextjs-routes via
npx nextjs-routes
has been removed in favor of automatic regeneration via withRoutes. See #63 for the motivation behind this change or to voice any concerns.
-
Support Next 13 app (beta) directory
-
Add
dir
option to support non standard NextJS project structures such as Nx:// next.config.js const withRoutes = require("nextjs-routes/config")({ dir: __dirname });
Thanks @alexgorbatchev for the contribution!
-
Accept path strings for static routes:
<Link href="/foo">Foo</Link>
Thanks @MariaSolOs for the contribution!
-
Use function overloads for
Link
androuter.push
androuter.replace
. This yields better hints for typos in pathnames:<Link href={{ pathname: "/foosy/[foo]" }}>Foo</Link>
Previously:
[tsserver 2322] [E] Type '"/foos/[foo]"' is not assignable to type '"/"'.
Now:
Type '"/foosy/[foo]"' is not assignable to type '"/api/hello" | "/bars/[bar]" | "/foos/[foo]" | "/"'. Did you mean '"/foos/[foo]"'?
(+2 other overload errors).
-
Export
Locale
fromnextjs-routes
.import { Locale } from "nextjs-routes";
Thanks @Asamsig for the contribution!
nextjs-routes
now generates route types for Nextjs i18n configuration. Eg the following next config:
module.exports = withRoutes({
i18n: {
defaultLocale: "de-DE",
locales: ["de-DE", "en-FR", "en-US"],
},
});
Will make locale
typed as 'de-DE' | 'en-FR' | 'en-US'
for Link
and useRouter
.
nextjs-routes
pageExtensions has been updated to respect multiple extensions such as.page.tsx
. In0.1.2
, only single extensions.tsx
were respected. This is now identical behavior to Next.js.
nextjs-routes
now respects pageExtensions fromnext.config.js
.
[ skipped ]
This release contains a breaking change, indicated by the minor version bump to 0.1.0. nextjs-routes
has not yet reached v1, but will follow semantic versioning once it does. Until then, minor version changes will be used to help flag breaking changes.
- Breaking change: the
withRoutes
import path and invocation has changed to better align with the general pattern in the Nextjs plugin ecosystem and to support configuration options, notably the newoutDir
option. It also now includes an ESM export to support usage innext.config.mjs
.
- const { withRoutes } = require("nextjs-routes/next-config.cjs");
+ const withRoutes = require("nextjs-routes/config")();
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
};
module.exports = withRoutes(nextConfig);
Note the import path has changed and the import itself has changed to function that is invoked with any configuration options. This provides better ergonomics for configuration options:
const withRoutes = require("nextjs-routes/config")({ outDir: "types" });
-
The type
RoutedQuery
has been added to retrieve theQuery
for a givenRoute
. This is useful as the context type parameter insidegetStaticProps
andgetServerSideProps
. Thanks @MariaSolOs for the contribution! -
withRoutes
now acceptsoutDir
as a configuration option to dictate wherenextjs-routes.d.ts
is generated. Thanks @MariaSolOs for the contribution!
- Deprecate direct invocation of nextjs-routes in favor of automatic regeneration via withRoutes. See #63 for the motivation behind this change or to voice any concerns.
- Add
route
runtime for generating type safe pathnames from aRoute
object
This can be used to fetch from API routes:
import { route } from "nextjs-routes";
fetch(route({ pathname: "/api/foos/[foo]", query: { foo: "foobar" } }));
Or for type safe redirects from getServerSideProps
:
import { route } from "nextjs-routes";
export const getServerSideProps: GetServerSideProps = async (context) => {
return {
redirect: {
destination: route({ pathname: "/foos/[foo]", query: { foo: "foobar" } }),
permanent: false,
},
};
};
- Move
chokidar
fromdevDependencies
todependencies
so it's installed automatically.
- Bug Fix: quote query segments in generated types. See #49 for more context.
- Bug Fix: don't generate routes for non navigable routes (
_error
,_app
,_document
). - Bug Fix: don't generate routes for test files that are co-located in pages directory. See #50 for more context.
-
query
is now typed asstring | string[] | undefined
instead ofstring | undefined
. -
nextjs-routes
can now be configured via yournext.config.js
to automatically regenerate types whenever your routes change:// next.config.js /** @type {import('next').NextConfig} */ const { withRoutes } = require("nextjs-routes/next-config.cjs"); const nextConfig = { reactStrictMode: true, }; module.exports = withRoutes(nextConfig);
This wiring will only run in Next.js' development server (eg
npx next dev
) andwithRoutes
will no-op in production.
- re-export types from
next/link
andnext/router
. - remove prettier as a peer dependency.
- enable src/pages for windows users.
- routes are now generated for routes that start with
_
._app
,_document
,_error
andmiddleware
are excluded. - gracefully handles missing pages directory and no pages.
- fixed prettier as an optional peer dependency
- nextjs-routes no longer adds types to the global type namespace. Previously,
Route
was available globally. Now, it must be imported:
import type { Route } from "nextjs-routes";
- query from
useRouter
is now correctly typed asstring | undefined
instead ofstring
. If you know the current route, you can supply a type argument to narrow required parameters tostring
, eg:
// if you have a page /foos/[foo].ts
const router = useRouter<"/foos/[foo]">();
// foo will be typed as a string, because the foo query parameter is required and thus will always be present.
const { foo } = router.query;
- Allow passing in
query
withoutpathname
to change current url parameters. router.query
can no longer beundefined
.
- Support search parameters. See #17 for more context.
- Removed reexports of
next/link
andnext/router
.
This means replacing imports of next/link
with nextjs-routes/link
and next/router
with nextjs-routes/router
is no longer necessary:
-import Link from "nextjs-routes/link";
+import Link from "next/link";
-import { useRouter } from 'nextjs-routes/router'
+import { useRouter } from 'next/router'
- Added windows support.