Skip to content

Commit

Permalink
Add support for params in header key/values (#10324)
Browse files Browse the repository at this point in the history
* Add support for params in header key/values

* Update todo

* Update with handling for named patterns

* Use compiled package

Co-authored-by: Joe Haddad <[email protected]>
  • Loading branch information
ijjk and Timer authored Apr 14, 2020
1 parent b1aa594 commit ab18c79
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 2 deletions.
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 'next/dist/compiled/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',
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

0 comments on commit ab18c79

Please sign in to comment.