Unverified Commit b51a0209 authored by Kiko Beats's avatar Kiko Beats Committed by GitHub

middleware: add request referrer support (#31343)

closes: https://github.com/vercel/next.js/issues/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 🤔
parent 66d9b4e1
...@@ -10,6 +10,7 @@ class BaseRequest extends Body implements Request { ...@@ -10,6 +10,7 @@ class BaseRequest extends Body implements Request {
credentials: RequestCredentials credentials: RequestCredentials
headers: Headers headers: Headers
method: string method: string
referrer: string
redirect: RequestRedirect redirect: RequestRedirect
url: NextURL url: NextURL
} }
...@@ -48,6 +49,7 @@ class BaseRequest extends Body implements Request { ...@@ -48,6 +49,7 @@ class BaseRequest extends Body implements Request {
init.credentials || getProp(input, 'credentials') || 'same-origin', init.credentials || getProp(input, 'credentials') || 'same-origin',
headers, headers,
method, method,
referrer: init.referrer || 'about:client',
redirect: init.redirect || getProp(input, 'redirect') || 'follow', redirect: init.redirect || getProp(input, 'redirect') || 'follow',
url: new NextURL(typeof input === 'string' ? input : input.url), url: new NextURL(typeof input === 'string' ? input : input.url),
} }
...@@ -65,6 +67,10 @@ class BaseRequest extends Body implements Request { ...@@ -65,6 +67,10 @@ class BaseRequest extends Body implements Request {
return this[INTERNALS].method return this[INTERNALS].method
} }
get referrer() {
return this[INTERNALS].referrer
}
get headers() { get headers() {
return this[INTERNALS].headers return this[INTERNALS].headers
} }
...@@ -97,10 +103,6 @@ class BaseRequest extends Body implements Request { ...@@ -97,10 +103,6 @@ class BaseRequest extends Body implements Request {
return notImplemented('Request', 'destination') return notImplemented('Request', 'destination')
} }
get referrer() {
return notImplemented('Request', 'referrer')
}
get referrerPolicy() { get referrerPolicy() {
return notImplemented('Request', 'referrerPolicy') return notImplemented('Request', 'referrerPolicy')
} }
......
...@@ -35,3 +35,13 @@ it('parses and reconstructs the URL alone', async () => { ...@@ -35,3 +35,13 @@ it('parses and reconstructs the URL alone', async () => {
it('throws when the URL is malformed', async () => { it('throws when the URL is malformed', async () => {
expect(() => new Request('meeeh')).toThrowError('Invalid URL') 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')
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment