Skip to content

Commit

Permalink
fix: headers rule matching with path prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
pieh committed May 23, 2024
1 parent c7c750b commit 9b17cb7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
29 changes: 24 additions & 5 deletions packages/gatsby/src/utils/adapter/create-headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,17 @@ const normalizePath = (input: string): string =>
input.endsWith(`/`) ? input : `${input}/`

export const createHeadersMatcher = (
headers: Array<IHeader> | undefined
headers: Array<IHeader> | undefined,
pathPrefix: string
): ((path: string, defaultHeaders: Headers) => Headers) => {
function stripPathPrefix(path: string): string {
if (pathPrefix && path.startsWith(pathPrefix)) {
path = path.slice(pathPrefix.length)
}

return path
}

// Split the incoming user headers into two buckets:
// - dynamicHeaders: Headers with dynamic paths (e.g. /* or /:tests)
// - staticHeaders: Headers with fully static paths (e.g. /static/)
Expand All @@ -27,13 +36,21 @@ export const createHeadersMatcher = (
}

for (const header of headers) {
if (header.source.includes(`:`) || header.source.includes(`*`)) {
const source = stripPathPrefix(header.source)
if (source.includes(`:`) || source.includes(`*`)) {
// rankRoute is the internal function that also "match" uses
const score = rankRoute(header.source)
const score = rankRoute(source)

dynamicHeaders.push({ ...header, score })
dynamicHeaders.push({
...header,
score,
source,
})
} else {
staticHeaders.set(normalizePath(header.source), header)
staticHeaders.set(normalizePath(source), {
...header,
source,
})
}
}

Expand All @@ -48,6 +65,8 @@ export const createHeadersMatcher = (
})

return (path: string, defaultHeaders: Headers): Headers => {
path = stripPathPrefix(path)

// Create a map of headers for the given path
// The key will be the header key. Since a key may only appear once in a map, the last header with the same key will win
const uniqueHeaders: Map<string, string> = new Map()
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/src/utils/adapter/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,10 +342,10 @@ function getRoutesManifest(): {
} {
const routes: Array<RouteWithScore> = []
const state = store.getState()
const createHeaders = createHeadersMatcher(state.config.headers)
const pathPrefix = state.program.prefixPaths
? state.config.pathPrefix ?? ``
: ``
const createHeaders = createHeadersMatcher(state.config.headers, pathPrefix)

const headerRoutes: HeaderRoutes = [...getDefaultHeaderRoutes(pathPrefix)]

Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/src/utils/page-ssr-module/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ type MaybePhantomActivity =
| ReturnType<typeof reporter.phantomActivity>
| undefined

const createHeaders = createHeadersMatcher(INLINED_HEADERS_CONFIG)
const createHeaders = createHeadersMatcher(INLINED_HEADERS_CONFIG, ``)

interface IGetDataBaseArgs {
pathName: string
Expand Down

0 comments on commit 9b17cb7

Please sign in to comment.