Skip to content

Commit 6392918

Browse files
committed
normalize index at build time
1 parent c4651fe commit 6392918

File tree

2 files changed

+32
-23
lines changed

2 files changed

+32
-23
lines changed

src/adapter/build/pages-and-app-handlers.ts

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,20 @@ const PAGES_AND_APP_FUNCTION_DIR = join(
2121
PAGES_AND_APP_FUNCTION_INTERNAL_NAME,
2222
)
2323

24-
// const abc: OneOfThePaths = 'asa(/abc/)dsa'
24+
// there is some inconsistency with pathnames sometimes being '/' and sometimes being '/index',
25+
// but handler seems to expect '/'
26+
function normalizeIndex(path: string): string {
27+
if (path === '/index') {
28+
return '/'
29+
}
30+
31+
return path.replace(
32+
// if Index is getServerSideProps weird things happen:
33+
// /_next/data/<build-id>/.json is produced instead of /_next/data/<build-id>/index.json
34+
/^\/_next\/data\/(?<buildId>[^/]+)\/\.json$/,
35+
'/_next/data/$<buildId>/index.json',
36+
)
37+
}
2538

2639
export async function onBuildComplete(
2740
nextAdapterContext: OnBuildCompleteContext,
@@ -46,24 +59,30 @@ export async function onBuildComplete(
4659
}
4760

4861
requiredFiles.add(output.filePath)
49-
pathnameToEntry[output.pathname] = relative(nextAdapterContext.repoRoot, output.filePath)
62+
pathnameToEntry[normalizeIndex(output.pathname)] = relative(
63+
nextAdapterContext.repoRoot,
64+
output.filePath,
65+
)
5066
}
5167
}
5268

5369
for (const prerender of nextAdapterContext.outputs.prerenders) {
54-
if (prerender.pathname in pathnameToEntry) {
55-
console.log('Skipping prerender, already have route:', prerender.pathname)
56-
} else if (prerender.parentOutputId in pathnameToEntry) {
70+
const normalizedPathname = normalizeIndex(prerender.pathname)
71+
const normalizedParentOutputId = normalizeIndex(prerender.parentOutputId)
72+
73+
if (normalizedPathname in pathnameToEntry) {
74+
console.log('Skipping prerender, already have route:', normalizedPathname)
75+
} else if (normalizedParentOutputId in pathnameToEntry) {
5776
// if we don't have routing for this route yet, add it
5877
console.log('prerender mapping', {
59-
from: prerender.pathname,
60-
to: prerender.parentOutputId,
78+
from: normalizedPathname,
79+
to: normalizedParentOutputId,
6180
})
62-
pathnameToEntry[prerender.pathname] = pathnameToEntry[prerender.parentOutputId]
81+
pathnameToEntry[normalizedPathname] = pathnameToEntry[normalizedParentOutputId]
6382
} else {
6483
console.warn('Could not find parent output for prerender:', {
65-
pathname: prerender,
66-
parentOutputId: prerender.parentOutputId,
84+
pathname: normalizedPathname,
85+
parentOutputId: normalizedParentOutputId,
6786
})
6887
}
6988
}

src/adapter/run/routing.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,6 @@ function selectRoutingPhasesRules(routingRules: RoutingRule[], phases: RoutingPh
8080

8181
let requestCounter = 0
8282

83-
function normalizeIndex(request: Request, prefix: string) {
84-
const currentURL = new URL(request.url)
85-
const { pathname } = currentURL
86-
if (pathname === '/') {
87-
const destURL = new URL('/index', currentURL)
88-
console.log(prefix, 'Normalizing "/" to "/index" for routing purposes')
89-
return new Request(destURL, request)
90-
}
91-
return request
92-
}
93-
9483
// eslint-disable-next-line max-params
9584
async function match(
9685
request: Request,
@@ -99,7 +88,7 @@ async function match(
9988
outputs: NetlifyAdapterContext['preparedOutputs'],
10089
prefix: string,
10190
) {
102-
let currentRequest = normalizeIndex(request, prefix)
91+
let currentRequest = request
10392
let maybeResponse: Response | undefined
10493

10594
const currentURL = new URL(currentRequest.url)
@@ -112,7 +101,8 @@ async function match(
112101
if (rule.match.type === 'static-asset-or-function') {
113102
let matchedType: 'static-asset' | 'function' | 'static-asset-alias' | null = null
114103

115-
// below assumes no overlap between static assets (files and aliases) and functions
104+
// below assumes no overlap between static assets (files and aliases) and functions so order of checks "doesn't matter"
105+
// unclear what should be precedence if there would ever be overlap
116106
if (outputs.staticAssets.includes(pathname)) {
117107
matchedType = 'static-asset'
118108
} else if (outputs.endpoints.includes(pathname)) {

0 commit comments

Comments
 (0)