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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

middleware: add request referrer support #31343

Merged
merged 2 commits into from Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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')
})