Skip to content

Commit 93b950a

Browse files
committed
feat: add additional routes to ssr paths
1 parent 47be7cb commit 93b950a

File tree

2 files changed

+60
-8
lines changed

2 files changed

+60
-8
lines changed

src/build/functions/server.ts

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,25 +101,61 @@ const applyTemplateVariables = (template: string, variables: Record<string, stri
101101
}
102102

103103
/** Convert Next.js route syntax to URLPattern syntax */
104-
const transformRoutePattern = (route: string): string => {
104+
const transformRoutePatterns = (route: string): string => {
105105
return route
106106
.replace(/\[\[\.\.\.(\w+)]]/g, ':$1*') // [[...slug]] -> :slug*
107107
.replace(/\[\.{3}(\w+)]/g, ':$1+') // [...slug] -> :slug+
108108
.replace(/\[(\w+)]/g, ':$1') // [id] -> :id
109109
}
110110

111-
/** Get's the content of the handler file that will be written to the lambda */
112-
const getHandlerFile = async (ctx: PluginContext): Promise<string> => {
113-
const templatesDir = join(ctx.pluginDir, 'dist/build/templates')
111+
const getRoutes = async (ctx: PluginContext) => {
112+
const internalRoutes = [
113+
'/_next/static/*',
114+
'/_next/data/*',
115+
'/_next/image/*',
116+
'/_next/postponed/*',
117+
]
114118

115119
const routesManifest = await ctx.getRoutesManifest()
116-
const routes = [...routesManifest.staticRoutes, ...routesManifest.dynamicRoutes]
117-
.map((route) => transformRoutePattern(route.page))
118-
.join("','")
120+
const staticRoutes = routesManifest.staticRoutes.map((route) => route.page)
121+
const dynamicRoutes = routesManifest.dynamicRoutes.map((route) => route.page)
122+
123+
// route.source conforms to the URLPattern syntax, which will work with our redirect engine
124+
// however this will be a superset of possible routes as it does not parse the
125+
// header/cookie/query matching that Next.js offers
126+
const redirects = routesManifest.redirects.map((route) => route.source)
127+
const rewrites = Array.isArray(routesManifest.rewrites)
128+
? routesManifest.rewrites.map((route) => route.source)
129+
: []
130+
131+
// this contains the static Route Handler routes
132+
const appPathRoutesManifest = await ctx.getAppPathRoutesManifest()
133+
const appRoutes = Object.values(appPathRoutesManifest)
134+
135+
// this contains the API handler routes
136+
const pagesManifest = await ctx.getPagesManifest()
137+
const pagesRoutes = Object.keys(pagesManifest)
138+
139+
return [
140+
...internalRoutes,
141+
...staticRoutes,
142+
...dynamicRoutes,
143+
...redirects,
144+
...rewrites,
145+
...appRoutes,
146+
...pagesRoutes,
147+
'/*', // retain the catch-all route for our initial testing
148+
].map(transformRoutePatterns)
149+
}
119150

151+
/** Get's the content of the handler file that will be written to the lambda */
152+
const getHandlerFile = async (ctx: PluginContext): Promise<string> => {
153+
const routes = await getRoutes(ctx)
154+
155+
const templatesDir = join(ctx.pluginDir, 'dist/build/templates')
120156
const templateVariables: Record<string, string> = {
121157
'{{useRegionalBlobs}}': ctx.useRegionalBlobs.toString(),
122-
'{{paths}}': routes,
158+
'{{paths}}': routes.join("','"),
123159
}
124160
// In this case it is a monorepo and we need to use a own template for it
125161
// as we have to change the process working directory

src/build/plugin-context.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,22 @@ export class PluginContext {
315315
return JSON.parse(await readFile(join(this.publishDir, 'routes-manifest.json'), 'utf-8'))
316316
}
317317

318+
/**
319+
* Get Next.js app path routes manifest from the build output
320+
*/
321+
async getAppPathRoutesManifest(): Promise<Record<string, string>> {
322+
return JSON.parse(
323+
await readFile(join(this.publishDir, 'app-path-routes-manifest.json'), 'utf-8'),
324+
)
325+
}
326+
327+
/**
328+
* Get Next.js app path routes manifest from the build output
329+
*/
330+
async getPagesManifest(): Promise<Record<string, string>> {
331+
return JSON.parse(await readFile(join(this.publishDir, 'server/pages-manifest.json'), 'utf-8'))
332+
}
333+
318334
#nextVersion: string | null | undefined = undefined
319335

320336
/**

0 commit comments

Comments
 (0)