Skip to content

Commit 75c0535

Browse files
authored
fix: correctly match params in edge runtime (#1896)
* fix: match params for edge runtime * chore: re-enable test * chore: snapidoo * chore: disable rewrite tests * chore: enable more tests * fix: match ef data routes * chore: add comments
1 parent aaf0548 commit 75c0535

File tree

12 files changed

+128
-66
lines changed

12 files changed

+128
-66
lines changed

demos/default/pages/edge/[id].js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
export const config = {
2+
runtime: 'experimental-edge',
3+
}
4+
5+
export default function Page(props) {
6+
return (
7+
<>
8+
<p id="page">/edge/[id]</p>
9+
<p id="props">{JSON.stringify(props)}</p>
10+
</>
11+
)
12+
}
13+
14+
export function getServerSideProps({ req, params, query }) {
15+
return {
16+
props: {
17+
url: req.url,
18+
query,
19+
params,
20+
now: Date.now(),
21+
},
22+
}
23+
}

package-lock.json

+23-33
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/runtime/src/helpers/edge.ts

+18
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ const getMiddlewareBundle = async ({
153153
const { publish } = netlifyConfig.build
154154
const chunks: Array<string> = [preamble]
155155

156+
chunks.push(`export const _DEFINITION = ${JSON.stringify(edgeFunctionDefinition)}`)
157+
156158
if ('wasm' in edgeFunctionDefinition) {
157159
for (const { name, filePath } of edgeFunctionDefinition.wasm) {
158160
const wasm = await fs.readFile(join(publish, filePath))
@@ -459,16 +461,22 @@ export const writeEdgeFunctions = async ({
459461
...matchers.map((matcher) => middlewareMatcherToEdgeFunctionDefinition(matcher, functionName)),
460462
)
461463
}
464+
// Functions (i.e. not middleware, but edge SSR and API routes)
462465
if (typeof middlewareManifest.functions === 'object') {
463466
// When using the app dir, we also need to check if the EF matches a page
464467
const appPathRoutesManifest = await loadAppPathRoutesManifest(netlifyConfig)
465468

469+
// A map of all route pages to their page regex. This is used for pages dir and appDir.
466470
const pageRegexMap = new Map(
467471
[...(routesManifest.dynamicRoutes || []), ...(routesManifest.staticRoutes || [])].map((route) => [
468472
route.page,
469473
route.regex,
470474
]),
471475
)
476+
// Create a map of pages-dir routes to their data route regex (appDir uses the same route as the HTML)
477+
const dataRoutesMap = new Map(
478+
[...(routesManifest.dataRoutes || [])].map((route) => [route.page, route.dataRouteRegex]),
479+
)
472480

473481
for (const edgeFunctionDefinition of Object.values(middlewareManifest.functions)) {
474482
usesEdge = true
@@ -491,6 +499,16 @@ export const writeEdgeFunctions = async ({
491499
// cache: "manual" is currently experimental, so we restrict it to sites that use experimental appDir
492500
cache: usesAppDir ? 'manual' : undefined,
493501
})
502+
// pages-dir page routes also have a data route. If there's a match, add an entry mapping that to the function too
503+
const dataRoute = dataRoutesMap.get(edgeFunctionDefinition.page)
504+
if (dataRoute) {
505+
manifest.functions.push({
506+
function: functionName,
507+
name: edgeFunctionDefinition.name,
508+
pattern: dataRoute,
509+
cache: usesAppDir ? 'manual' : undefined,
510+
})
511+
}
494512
}
495513
}
496514
if (usesEdge) {
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/**
22
* This placeholder is replaced with the compiled Next.js bundle at build time
33
* @param {Object} props
4-
* @param {import("./runtime.ts").RequestData} props.request
4+
* @param {import("./function-runtime.ts").RequestData} props.request
55
* @returns {Promise<import("../edge-shared/utils.ts").FetchEventResult>}
66
*/
77
export default async ({ request }) => {}
8+
export const _DEFINITION = { env: [], files: [], page: 'placeholder', name: 'pages_placeholder', matchers: [] }

packages/runtime/src/templates/edge/function-runtime.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Context } from 'https://edge.netlify.com'
22
// Available at build time
3-
import edgeFunction from './bundle.js'
3+
import { _DEFINITION as edgeFunctionDefinition, default as edgeFunction } from './bundle.js'
44
import { buildNextRequest, buildResponse, redirectTrailingSlash } from '../edge-shared/utils.ts'
55
import nextConfig from '../edge-shared/nextConfig.json' assert { type: 'json' }
66

@@ -11,6 +11,7 @@ const handler = async (req: Request, context: Context) => {
1111
return redirect
1212
}
1313
const request = buildNextRequest(req, context, nextConfig)
14+
request.headers['x-matched-path'] ||= edgeFunctionDefinition.page
1415
try {
1516
const result = await edgeFunction({ request })
1617
return buildResponse({ result, request: req, context })

0 commit comments

Comments
 (0)