Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for params in header key/values #10324

Merged
merged 9 commits into from
Apr 14, 2020
14 changes: 12 additions & 2 deletions packages/next/next-server/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ import {
} from './spr-cache'
import { execOnce } from '../lib/utils'
import { isBlockedPage } from './utils'
import { compile as compilePathToRegex } from 'path-to-regexp'
import { loadEnvConfig } from '../../lib/load-env-config'

const getCustomRouteMatcher = pathMatch(true)
Expand Down Expand Up @@ -483,9 +484,18 @@ export default class Server {
match: route.match,
type: route.type,
name: `${route.type} ${route.source} header route`,
fn: async (_req, res, _params, _parsedUrl) => {
fn: async (_req, res, params, _parsedUrl) => {
for (const header of (route as Header).headers) {
res.setHeader(header.key, header.value)
let { key, value } = header
if (key.includes(':')) {
// see `prepareDestination` util for explanation for
// `validate: false` being used
key = compilePathToRegex(key, { validate: false })(params)
}
if (value.includes(':')) {
value = compilePathToRegex(value, { validate: false })(params)
}
res.setHeader(key, value)
}
return { finished: false }
},
Expand Down
26 changes: 26 additions & 0 deletions test/integration/custom-routes/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,19 @@ module.exports = {
},
],
},
{
source: '/my-other-header/:path',
headers: [
{
key: 'x-path',
value: ':path',
},
{
key: 'some:path',
value: 'hi',
},
],
},
{
source: '/:path*',
headers: [
Expand All @@ -226,6 +239,19 @@ module.exports = {
},
],
},
{
source: '/named-pattern/:path(.*)',
headers: [
{
key: 'x-something',
value: 'value=:path',
},
{
key: 'path-:path',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the practical case for setting the header name to a dynamic value?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Depending on their set-up a user might want to return the original path as a header for rewrite/proxying scenarios. We mainly added this in @now/routing-utils for consistency so added this here to match that behavior

value: 'end',
},
],
},
]
},
},
Expand Down
40 changes: 40 additions & 0 deletions test/integration/custom-routes/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,18 @@ const runTests = (isDev = false) => {
expect(res.headers.get('x-second-header')).toBe('second')
})

it('should apply params for header key/values', async () => {
const res = await fetchViaHTTP(appPort, '/my-other-header/first')
expect(res.headers.get('x-path')).toBe('first')
expect(res.headers.get('somefirst')).toBe('hi')
})

it('should support named pattern for header key/values', async () => {
const res = await fetchViaHTTP(appPort, '/named-pattern/hello')
expect(res.headers.get('x-something')).toBe('value=hello')
expect(res.headers.get('path-hello')).toBe('end')
})

it('should support proxying to external resource', async () => {
const res = await fetchViaHTTP(appPort, '/proxy-me/first?keep=me&and=me')
expect(res.status).toBe(200)
Expand Down Expand Up @@ -601,6 +613,20 @@ const runTests = (isDev = false) => {
regex: normalizeRegEx('^\\/my-headers(?:\\/(.*))$'),
source: '/my-headers/(.*)',
},
{
headers: [
{
key: 'x-path',
value: ':path',
},
{
key: 'some:path',
value: 'hi',
},
],
regex: normalizeRegEx('^\\/my-other-header(?:\\/([^\\/]+?))$'),
source: '/my-other-header/:path',
},
{
headers: [
{
Expand All @@ -613,6 +639,20 @@ const runTests = (isDev = false) => {
),
source: '/:path*',
},
{
headers: [
{
key: 'x-something',
value: 'value=:path',
},
{
key: 'path-:path',
value: 'end',
},
],
regex: normalizeRegEx('^\\/named-pattern(?:\\/(.*))$'),
source: '/named-pattern/:path(.*)',
},
],
rewrites: [
{
Expand Down