Skip to content

Commit efcbf69

Browse files
committed
chore: refactor header control
1 parent 38c8f82 commit efcbf69

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

src/helpers/headers.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { HandlerEvent } from '@netlify/functions'
12
import type { NextConfigComplete } from 'next/dist/server/config-shared.js'
23

34
export interface NetlifyVaryHeaderBuilder {
@@ -42,12 +43,7 @@ const removeSMaxAgeAndStaleWhileRevalidate = (headerValue: string): string =>
4243
})
4344
.join(`,`)
4445

45-
export const handleVary = (
46-
headers: Record<string, string>,
47-
eventPath: string,
48-
basePath: string,
49-
autoDetectedLocales: string[],
50-
) => {
46+
export const handleVary = (headers: Record<string, string>, event: HandlerEvent, nextConfig: NextConfigComplete) => {
5147
const netlifyVaryBuilder: NetlifyVaryHeaderBuilder = {
5248
headers: [],
5349
languages: [],
@@ -58,8 +54,13 @@ export const handleVary = (
5854
netlifyVaryBuilder.headers.push(...getDirectives(headers.vary))
5955
}
6056

57+
const autoDetectedLocales = getAutoDetectedLocales(nextConfig)
58+
6159
if (autoDetectedLocales.length > 1) {
62-
const logicalPath = basePath && eventPath.startsWith(basePath) ? eventPath.slice(basePath.length) : eventPath
60+
const logicalPath =
61+
nextConfig.basePath && event.path.startsWith(nextConfig.basePath)
62+
? event.path.slice(nextConfig.basePath.length)
63+
: event.path
6364

6465
if (logicalPath === `/`) {
6566
netlifyVaryBuilder.languages.push(...autoDetectedLocales)

src/templates/server-handler.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@ import type { Handler, HandlerContext, HandlerEvent } from '@netlify/functions'
66
import { Bridge } from '@vercel/node-bridge/bridge.js'
77
import type { NextConfigComplete } from 'next/dist/server/config-shared.js'
88

9-
import { getAutoDetectedLocales, handleCacheControl, handleVary } from '../helpers/headers.js'
9+
import { handleCacheControl, handleVary } from '../helpers/headers.js'
1010

1111
const require = createRequire(import.meta.url)
1212

13+
/* these dependencies are generated during the build */
14+
// eslint-disable-next-line import/order
1315
const { getRequestHandlers } = require('next/dist/server/lib/start-server.js')
14-
1516
const requiredServerFiles = require('../../.next/required-server-files.json')
1617

1718
process.env.__NEXT_PRIVATE_STANDALONE_CONFIG = JSON.stringify(requiredServerFiles.config)
1819

1920
const nextConfig = requiredServerFiles.config as NextConfigComplete
20-
const autoDetectedLocales = getAutoDetectedLocales(nextConfig)
2121

2222
const __dirname = fileURLToPath(new URL('../..', import.meta.url))
2323

@@ -60,7 +60,7 @@ export const handler: Handler = async function (event: HandlerEvent, context: Ha
6060
console.log('Next server response:', JSON.stringify(response, null, 2))
6161

6262
handleCacheControl(headers)
63-
handleVary(headers, event.path, nextConfig.basePath, autoDetectedLocales)
63+
handleVary(headers, event, nextConfig)
6464

6565
return {
6666
...result,

0 commit comments

Comments
 (0)