Skip to content

Commit 0002647

Browse files
Merge branch 'main' into tn/add-middlewareMatcher-missing
2 parents a4c5abd + 2e5a728 commit 0002647

File tree

12 files changed

+193
-45
lines changed

12 files changed

+193
-45
lines changed

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

Lines changed: 23 additions & 0 deletions
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

Lines changed: 88 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/runtime/src/helpers/edge.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ const getMiddlewareBundle = async ({
154154
const { publish } = netlifyConfig.build
155155
const chunks: Array<string> = [preamble]
156156

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

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

474482
for (const edgeFunctionDefinition of Object.values(middlewareManifest.functions)) {
475483
usesEdge = true
@@ -492,6 +500,16 @@ export const writeEdgeFunctions = async ({
492500
// cache: "manual" is currently experimental, so we restrict it to sites that use experimental appDir
493501
cache: usesAppDir ? 'manual' : undefined,
494502
})
503+
// pages-dir page routes also have a data route. If there's a match, add an entry mapping that to the function too
504+
const dataRoute = dataRoutesMap.get(edgeFunctionDefinition.page)
505+
if (dataRoute) {
506+
manifest.functions.push({
507+
function: functionName,
508+
name: edgeFunctionDefinition.name,
509+
pattern: dataRoute,
510+
cache: usesAppDir ? 'manual' : undefined,
511+
})
512+
}
495513
}
496514
}
497515
if (usesEdge) {
Lines changed: 2 additions & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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 })

test/__snapshots__/index.spec.js.snap

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Array [
2828
".next/server/pages/api/shows/[...params].js",
2929
".next/server/pages/api/shows/[id].js",
3030
".next/server/pages/deep/import.js",
31+
".next/server/pages/edge/[id].js",
3132
".next/server/pages/getServerSideProps/[id].js",
3233
".next/server/pages/getServerSideProps/all/[[...slug]].js",
3334
".next/server/pages/getServerSideProps/file.js",
@@ -122,6 +123,7 @@ exports.resolvePages = () => {
122123
require.resolve('../../../.next/server/pages/api/shows/[...params].js')
123124
require.resolve('../../../.next/server/pages/api/shows/[id].js')
124125
require.resolve('../../../.next/server/pages/deep/import.js')
126+
require.resolve('../../../.next/server/pages/edge/[id].js')
125127
require.resolve('../../../.next/server/pages/getServerSideProps/[id].js')
126128
require.resolve('../../../.next/server/pages/getServerSideProps/all/[[...slug]].js')
127129
require.resolve('../../../.next/server/pages/getServerSideProps/file.js')
@@ -180,6 +182,7 @@ exports.resolvePages = () => {
180182
require.resolve('../../../.next/server/pages/api/shows/[...params].js')
181183
require.resolve('../../../.next/server/pages/api/shows/[id].js')
182184
require.resolve('../../../.next/server/pages/deep/import.js')
185+
require.resolve('../../../.next/server/pages/edge/[id].js')
183186
require.resolve('../../../.next/server/pages/getServerSideProps/[id].js')
184187
require.resolve('../../../.next/server/pages/getServerSideProps/all/[[...slug]].js')
185188
require.resolve('../../../.next/server/pages/getServerSideProps/file.js')
@@ -238,6 +241,7 @@ exports.resolvePages = () => {
238241
require.resolve('../../../web/.next/server/pages/api/shows/[...params].js')
239242
require.resolve('../../../web/.next/server/pages/api/shows/[id].js')
240243
require.resolve('../../../web/.next/server/pages/deep/import.js')
244+
require.resolve('../../../web/.next/server/pages/edge/[id].js')
241245
require.resolve('../../../web/.next/server/pages/getServerSideProps/[id].js')
242246
require.resolve('../../../web/.next/server/pages/getServerSideProps/all/[[...slug]].js')
243247
require.resolve('../../../web/.next/server/pages/getServerSideProps/file.js')
@@ -296,6 +300,7 @@ exports.resolvePages = () => {
296300
require.resolve('../../../web/.next/server/pages/api/shows/[...params].js')
297301
require.resolve('../../../web/.next/server/pages/api/shows/[id].js')
298302
require.resolve('../../../web/.next/server/pages/deep/import.js')
303+
require.resolve('../../../web/.next/server/pages/edge/[id].js')
299304
require.resolve('../../../web/.next/server/pages/getServerSideProps/[id].js')
300305
require.resolve('../../../web/.next/server/pages/getServerSideProps/all/[[...slug]].js')
301306
require.resolve('../../../web/.next/server/pages/getServerSideProps/file.js')
@@ -671,6 +676,12 @@ Array [
671676
"status": 200,
672677
"to": "/.netlify/functions/___netlify-handler",
673678
},
679+
Object {
680+
"force": false,
681+
"from": "/_next/data/build-id/en/edge/:id.json",
682+
"status": 200,
683+
"to": "/.netlify/functions/___netlify-handler",
684+
},
674685
Object {
675686
"force": false,
676687
"from": "/_next/data/build-id/en/env.json",
@@ -911,6 +922,12 @@ Array [
911922
"status": 200,
912923
"to": "/.netlify/functions/___netlify-handler",
913924
},
925+
Object {
926+
"force": false,
927+
"from": "/_next/data/build-id/es/edge/:id.json",
928+
"status": 200,
929+
"to": "/.netlify/functions/___netlify-handler",
930+
},
914931
Object {
915932
"force": false,
916933
"from": "/_next/data/build-id/es/env.json",
@@ -1109,6 +1126,12 @@ Array [
11091126
"status": 200,
11101127
"to": "/.netlify/functions/___netlify-handler",
11111128
},
1129+
Object {
1130+
"force": false,
1131+
"from": "/_next/data/build-id/fr/edge/:id.json",
1132+
"status": 200,
1133+
"to": "/.netlify/functions/___netlify-handler",
1134+
},
11121135
Object {
11131136
"force": false,
11141137
"from": "/_next/data/build-id/fr/env.json",
@@ -1443,6 +1466,12 @@ Array [
14431466
"status": 200,
14441467
"to": "/.netlify/functions/___netlify-handler",
14451468
},
1469+
Object {
1470+
"force": false,
1471+
"from": "/edge/:id",
1472+
"status": 200,
1473+
"to": "/.netlify/functions/___netlify-handler",
1474+
},
14461475
Object {
14471476
"force": false,
14481477
"from": "/env",
@@ -1503,6 +1532,12 @@ Array [
15031532
"status": 200,
15041533
"to": "/.netlify/functions/___netlify-handler",
15051534
},
1535+
Object {
1536+
"force": false,
1537+
"from": "/es/edge/:id",
1538+
"status": 200,
1539+
"to": "/.netlify/functions/___netlify-handler",
1540+
},
15061541
Object {
15071542
"force": false,
15081543
"from": "/es/env",
@@ -1731,6 +1766,12 @@ Array [
17311766
"status": 200,
17321767
"to": "/.netlify/functions/___netlify-handler",
17331768
},
1769+
Object {
1770+
"force": false,
1771+
"from": "/fr/edge/:id",
1772+
"status": 200,
1773+
"to": "/.netlify/functions/___netlify-handler",
1774+
},
17341775
Object {
17351776
"force": false,
17361777
"from": "/fr/env",

test/e2e/app-dir/app-static.test.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ describe('app-dir static/dynamic handling', () => {
220220
}
221221
})
222222

223-
usuallySkip('should honor dynamic = "force-static" correctly (lazy)', async () => {
223+
it('should honor dynamic = "force-static" correctly (lazy)', async () => {
224224
const res = await fetchViaHTTP(next.url, '/force-static/random')
225225
expect(res.status).toBe(200)
226226

@@ -241,8 +241,7 @@ describe('app-dir static/dynamic handling', () => {
241241
expect(firstTime).toBe($2('#now').text())
242242
}
243243
})
244-
// NTL Skip
245-
it.skip('should handle dynamicParams: false correctly', async () => {
244+
it('should handle dynamicParams: false correctly', async () => {
246245
const validParams = ['tim', 'seb', 'styfle']
247246

248247
for (const param of validParams) {
@@ -315,8 +314,7 @@ describe('app-dir static/dynamic handling', () => {
315314
expect($('#page').text()).toBe('/blog/[author]/[slug]')
316315
}
317316
})
318-
// NTL Skip
319-
it.skip('should navigate to static path correctly', async () => {
317+
it('should navigate to static path correctly', async () => {
320318
const browser = await webdriver(next.url, '/blog/tim')
321319
await browser.eval('window.beforeNav = 1')
322320

0 commit comments

Comments
 (0)