Skip to content

Commit

Permalink
middleware: add request referrer support (#31343)
Browse files Browse the repository at this point in the history
closes: #30353

According with spec, `'about:client'` is the default value is the user doesn't provide it.

It needs to add a test there, looks like there no unit tests for these classes 🤔
  • Loading branch information
Kikobeats committed Nov 15, 2021
1 parent 66d9b4e commit b51a020
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
10 changes: 6 additions & 4 deletions packages/next/server/web/spec-compliant/request.ts
Expand Up @@ -10,6 +10,7 @@ class BaseRequest extends Body implements Request {
credentials: RequestCredentials
headers: Headers
method: string
referrer: string
redirect: RequestRedirect
url: NextURL
}
Expand Down Expand Up @@ -48,6 +49,7 @@ class BaseRequest extends Body implements Request {
init.credentials || getProp(input, 'credentials') || 'same-origin',
headers,
method,
referrer: init.referrer || 'about:client',
redirect: init.redirect || getProp(input, 'redirect') || 'follow',
url: new NextURL(typeof input === 'string' ? input : input.url),
}
Expand All @@ -65,6 +67,10 @@ class BaseRequest extends Body implements Request {
return this[INTERNALS].method
}

get referrer() {
return this[INTERNALS].referrer
}

get headers() {
return this[INTERNALS].headers
}
Expand Down Expand Up @@ -97,10 +103,6 @@ class BaseRequest extends Body implements Request {
return notImplemented('Request', 'destination')
}

get referrer() {
return notImplemented('Request', 'referrer')
}

get referrerPolicy() {
return notImplemented('Request', 'referrerPolicy')
}
Expand Down
10 changes: 10 additions & 0 deletions test/unit/web-runtime/request.test.ts
Expand Up @@ -35,3 +35,13 @@ it('parses and reconstructs the URL alone', async () => {
it('throws when the URL is malformed', async () => {
expect(() => new Request('meeeh')).toThrowError('Invalid URL')
})

it('Request.referrer is `about:client` by default', async () => {
const request = new Request('https://vercel.com')
expect(request.referrer).toBe('about:client')
})

it('Request.referrer can be customized', async () => {
const request = new Request('https://vercel.com', { referrer: 'client' })
expect(request.referrer).toBe('client')
})

0 comments on commit b51a020

Please sign in to comment.