Apply middleware patches

parent 58dcf304
...@@ -155,6 +155,12 @@ export function getDefineEnv({ ...@@ -155,6 +155,12 @@ export function getDefineEnv({
'process.env.__NEXT_OPTIMISTIC_CLIENT_CACHE': JSON.stringify( 'process.env.__NEXT_OPTIMISTIC_CLIENT_CACHE': JSON.stringify(
config.experimental.optimisticClientCache config.experimental.optimisticClientCache
), ),
'process.env.__NEXT_NO_MIDDLEWARE_URL_NORMALIZE': JSON.stringify(
config.experimental.skipMiddlewareUrlNormalize
),
'process.env.__NEXT_ALLOW_MIDDLEWARE_RESPONSE_BODY': JSON.stringify(
config.experimental.allowMiddlewareResponseBody
),
'process.env.__NEXT_CROSS_ORIGIN': JSON.stringify(config.crossOrigin), 'process.env.__NEXT_CROSS_ORIGIN': JSON.stringify(config.crossOrigin),
'process.browser': JSON.stringify(isClient), 'process.browser': JSON.stringify(isClient),
'process.env.__NEXT_TEST_MODE': JSON.stringify( 'process.env.__NEXT_TEST_MODE': JSON.stringify(
...@@ -1779,6 +1785,8 @@ export default async function getBaseWebpackConfig( ...@@ -1779,6 +1785,8 @@ export default async function getBaseWebpackConfig(
new MiddlewarePlugin({ new MiddlewarePlugin({
dev, dev,
sriEnabled: !dev && !!config.experimental.sri?.algorithm, sriEnabled: !dev && !!config.experimental.sri?.algorithm,
allowMiddlewareResponseBody:
!!config.experimental.allowMiddlewareResponseBody,
}), }),
isClient && isClient &&
new BuildManifestPlugin({ new BuildManifestPlugin({
......
...@@ -335,12 +335,14 @@ function getCodeAnalyzer(params: { ...@@ -335,12 +335,14 @@ function getCodeAnalyzer(params: {
dev: boolean dev: boolean
compiler: webpack.Compiler compiler: webpack.Compiler
compilation: webpack.Compilation compilation: webpack.Compilation
allowMiddlewareResponseBody: boolean
}) { }) {
return (parser: webpack.javascript.JavascriptParser) => { return (parser: webpack.javascript.JavascriptParser) => {
const { const {
dev, dev,
compiler: { webpack: wp }, compiler: { webpack: wp },
compilation, compilation,
allowMiddlewareResponseBody,
} = params } = params
const { hooks } = parser const { hooks } = parser
...@@ -557,8 +559,10 @@ Learn More: https://nextjs.org/docs/messages/node-module-in-edge-runtime`, ...@@ -557,8 +559,10 @@ Learn More: https://nextjs.org/docs/messages/node-module-in-edge-runtime`,
.for(`${prefix}WebAssembly.instantiate`) .for(`${prefix}WebAssembly.instantiate`)
.tap(NAME, handleWrapWasmInstantiateExpression) .tap(NAME, handleWrapWasmInstantiateExpression)
} }
if (!allowMiddlewareResponseBody) {
hooks.new.for('Response').tap(NAME, handleNewResponseExpression) hooks.new.for('Response').tap(NAME, handleNewResponseExpression)
hooks.new.for('NextResponse').tap(NAME, handleNewResponseExpression) hooks.new.for('NextResponse').tap(NAME, handleNewResponseExpression)
}
hooks.callMemberChain.for('process').tap(NAME, handleCallMemberChain) hooks.callMemberChain.for('process').tap(NAME, handleCallMemberChain)
hooks.expressionMemberChain.for('process').tap(NAME, handleCallMemberChain) hooks.expressionMemberChain.for('process').tap(NAME, handleCallMemberChain)
hooks.importCall.tap(NAME, handleImport) hooks.importCall.tap(NAME, handleImport)
...@@ -797,10 +801,19 @@ function getExtractMetadata(params: { ...@@ -797,10 +801,19 @@ function getExtractMetadata(params: {
export default class MiddlewarePlugin { export default class MiddlewarePlugin {
private readonly dev: boolean private readonly dev: boolean
private readonly sriEnabled: boolean private readonly sriEnabled: boolean
private readonly allowMiddlewareResponseBody: boolean
constructor({ dev, sriEnabled }: { dev: boolean; sriEnabled: boolean }) { constructor({
dev,
sriEnabled,
allowMiddlewareResponseBody,
}: {
dev: boolean
sriEnabled: boolean
allowMiddlewareResponseBody: boolean
}) {
this.dev = dev this.dev = dev
this.sriEnabled = sriEnabled this.sriEnabled = sriEnabled
this.allowMiddlewareResponseBody = allowMiddlewareResponseBody
} }
public apply(compiler: webpack.Compiler) { public apply(compiler: webpack.Compiler) {
...@@ -813,6 +826,7 @@ export default class MiddlewarePlugin { ...@@ -813,6 +826,7 @@ export default class MiddlewarePlugin {
dev: this.dev, dev: this.dev,
compiler, compiler,
compilation, compilation,
allowMiddlewareResponseBody: this.allowMiddlewareResponseBody,
}) })
hooks.parser.for('javascript/auto').tap(NAME, codeAnalyzer) hooks.parser.for('javascript/auto').tap(NAME, codeAnalyzer)
hooks.parser.for('javascript/dynamic').tap(NAME, codeAnalyzer) hooks.parser.for('javascript/dynamic').tap(NAME, codeAnalyzer)
......
...@@ -624,6 +624,7 @@ export default async function loadCustomRoutes( ...@@ -624,6 +624,7 @@ export default async function loadCustomRoutes(
) )
} }
if (!config.experimental?.skipTrailingSlashRedirect) {
if (config.trailingSlash) { if (config.trailingSlash) {
redirects.unshift( redirects.unshift(
{ {
...@@ -670,6 +671,7 @@ export default async function loadCustomRoutes( ...@@ -670,6 +671,7 @@ export default async function loadCustomRoutes(
} as Redirect) } as Redirect)
} }
} }
}
return { return {
headers, headers,
......
...@@ -434,6 +434,33 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -434,6 +434,33 @@ export default abstract class Server<ServerOptions extends Options = Options> {
parsedUrl?: NextUrlWithParsedQuery parsedUrl?: NextUrlWithParsedQuery
): Promise<void> { ): Promise<void> {
try { try {
// ensure cookies set in middleware are merged and
// not overridden by API routes/getServerSideProps
const _res = (res as any).originalResponse || res
const origSetHeader = _res.setHeader.bind(_res)
_res.setHeader = (name: string, val: string | string[]) => {
if (name.toLowerCase() === 'set-cookie') {
const middlewareValue = getRequestMeta(req, '_nextMiddlewareCookie')
if (
!middlewareValue ||
!Array.isArray(val) ||
!val.every((item, idx) => item === middlewareValue[idx])
) {
val = [
...(middlewareValue || []),
...(typeof val === 'string'
? [val]
: Array.isArray(val)
? val
: []),
]
}
}
return origSetHeader(name, val)
}
const urlParts = (req.url || '').split('?') const urlParts = (req.url || '').split('?')
const urlNoQuery = urlParts[0] const urlNoQuery = urlParts[0]
......
...@@ -222,6 +222,9 @@ const configSchema = { ...@@ -222,6 +222,9 @@ const configSchema = {
adjustFontFallbacks: { adjustFontFallbacks: {
type: 'boolean', type: 'boolean',
}, },
allowMiddlewareResponseBody: {
type: 'boolean',
},
amp: { amp: {
additionalProperties: false, additionalProperties: false,
properties: { properties: {
...@@ -348,6 +351,12 @@ const configSchema = { ...@@ -348,6 +351,12 @@ const configSchema = {
sharedPool: { sharedPool: {
type: 'boolean', type: 'boolean',
}, },
skipMiddlewareUrlNormalize: {
type: 'boolean',
},
skipTrailingSlashRedirect: {
type: 'boolean',
},
sri: { sri: {
properties: { properties: {
algorithm: { algorithm: {
......
...@@ -78,6 +78,9 @@ export interface NextJsWebpackConfig { ...@@ -78,6 +78,9 @@ export interface NextJsWebpackConfig {
} }
export interface ExperimentalConfig { export interface ExperimentalConfig {
allowMiddlewareResponseBody?: boolean
skipMiddlewareUrlNormalize?: boolean
skipTrailingSlashRedirect?: boolean
optimisticClientCache?: boolean optimisticClientCache?: boolean
legacyBrowsers?: boolean legacyBrowsers?: boolean
browsersListForSwc?: boolean browsersListForSwc?: boolean
......
...@@ -83,7 +83,7 @@ import { normalizePagePath } from '../shared/lib/page-path/normalize-page-path' ...@@ -83,7 +83,7 @@ import { normalizePagePath } from '../shared/lib/page-path/normalize-page-path'
import { loadComponents } from './load-components' import { loadComponents } from './load-components'
import isError, { getProperError } from '../lib/is-error' import isError, { getProperError } from '../lib/is-error'
import { FontManifest } from './font-utils' import { FontManifest } from './font-utils'
import { toNodeHeaders } from './web/utils' import { splitCookiesString, toNodeHeaders } from './web/utils'
import { relativizeURL } from '../shared/lib/router/utils/relativize-url' import { relativizeURL } from '../shared/lib/router/utils/relativize-url'
import { prepareDestination } from '../shared/lib/router/utils/prepare-destination' import { prepareDestination } from '../shared/lib/router/utils/prepare-destination'
import { normalizeLocalePath } from '../shared/lib/i18n/normalize-locale-path' import { normalizeLocalePath } from '../shared/lib/i18n/normalize-locale-path'
...@@ -1051,9 +1051,17 @@ export default class NextNodeServer extends BaseServer { ...@@ -1051,9 +1051,17 @@ export default class NextNodeServer extends BaseServer {
name: '_next/data catchall', name: '_next/data catchall',
check: true, check: true,
fn: async (req, res, params, _parsedUrl) => { fn: async (req, res, params, _parsedUrl) => {
const isNextDataNormalizing = getRequestMeta(
req,
'_nextDataNormalizing'
)
// Make sure to 404 for /_next/data/ itself and // Make sure to 404 for /_next/data/ itself and
// we also want to 404 if the buildId isn't correct // we also want to 404 if the buildId isn't correct
if (!params.path || params.path[0] !== this.buildId) { if (!params.path || params.path[0] !== this.buildId) {
if (isNextDataNormalizing) {
return { finished: false }
}
await this.render404(req, res, _parsedUrl) await this.render404(req, res, _parsedUrl)
return { return {
finished: true, finished: true,
...@@ -1797,6 +1805,14 @@ export default class NextNodeServer extends BaseServer { ...@@ -1797,6 +1805,14 @@ export default class NextNodeServer extends BaseServer {
} else { } else {
for (let [key, value] of allHeaders) { for (let [key, value] of allHeaders) {
result.response.headers.set(key, value) result.response.headers.set(key, value)
if (key.toLowerCase() === 'set-cookie') {
addRequestMeta(
params.request,
'_nextMiddlewareCookie',
splitCookiesString(value)
)
}
} }
} }
...@@ -2105,8 +2121,13 @@ export default class NextNodeServer extends BaseServer { ...@@ -2105,8 +2121,13 @@ export default class NextNodeServer extends BaseServer {
params.res.statusCode = result.response.status params.res.statusCode = result.response.status
params.res.statusMessage = result.response.statusText params.res.statusMessage = result.response.statusText
result.response.headers.forEach((value, key) => { result.response.headers.forEach((value: string, key) => {
// the append handling is special cased for `set-cookie`
if (key.toLowerCase() === 'set-cookie') {
params.res.setHeader(key, value)
} else {
params.res.appendHeader(key, value) params.res.appendHeader(key, value)
}
}) })
if (result.response.body) { if (result.response.body) {
......
...@@ -22,6 +22,8 @@ export interface RequestMeta { ...@@ -22,6 +22,8 @@ export interface RequestMeta {
_nextHadBasePath?: boolean _nextHadBasePath?: boolean
_nextRewroteUrl?: string _nextRewroteUrl?: string
_protocol?: string _protocol?: string
_nextMiddlewareCookie?: string[]
_nextDataNormalizing?: boolean
} }
export function getRequestMeta( export function getRequestMeta(
......
...@@ -7,7 +7,11 @@ import type { ...@@ -7,7 +7,11 @@ import type {
} from '../shared/lib/router/utils/route-matcher' } from '../shared/lib/router/utils/route-matcher'
import type { RouteHas } from '../lib/load-custom-routes' import type { RouteHas } from '../lib/load-custom-routes'
import { getNextInternalQuery, NextUrlWithParsedQuery } from './request-meta' import {
addRequestMeta,
getNextInternalQuery,
NextUrlWithParsedQuery,
} from './request-meta'
import { getPathMatch } from '../shared/lib/router/utils/path-match' import { getPathMatch } from '../shared/lib/router/utils/path-match'
import { removeTrailingSlash } from '../shared/lib/router/utils/remove-trailing-slash' import { removeTrailingSlash } from '../shared/lib/router/utils/remove-trailing-slash'
import { normalizeLocalePath } from '../shared/lib/i18n/normalize-locale-path' import { normalizeLocalePath } from '../shared/lib/i18n/normalize-locale-path'
...@@ -189,7 +193,11 @@ export default class Router { ...@@ -189,7 +193,11 @@ export default class Router {
...(middlewareCatchAllRoute ...(middlewareCatchAllRoute
? this.fsRoutes ? this.fsRoutes
.filter((route) => route.name === '_next/data catchall') .filter((route) => route.name === '_next/data catchall')
.map((route) => ({ ...route, check: false })) .map((route) => ({
...route,
name: '_next/data normalizing',
check: false,
}))
: []), : []),
...this.headers, ...this.headers,
...this.redirects, ...this.redirects,
...@@ -433,6 +441,11 @@ export default class Router { ...@@ -433,6 +441,11 @@ export default class Router {
} }
if (params) { if (params) {
const isNextDataNormalizing = route.name === '_next/data normalizing'
if (isNextDataNormalizing) {
addRequestMeta(req, '_nextDataNormalizing', true)
}
parsedUrlUpdated.pathname = matchPathname parsedUrlUpdated.pathname = matchPathname
const result = await route.fn( const result = await route.fn(
req, req,
...@@ -441,6 +454,9 @@ export default class Router { ...@@ -441,6 +454,9 @@ export default class Router {
parsedUrlUpdated, parsedUrlUpdated,
upgradeHead upgradeHead
) )
if (isNextDataNormalizing) {
addRequestMeta(req, '_nextDataNormalizing', false)
}
if (result.finished) { if (result.finished) {
return true return true
} }
......
...@@ -110,12 +110,14 @@ export async function adapter(params: { ...@@ -110,12 +110,14 @@ export async function adapter(params: {
nextConfig: params.request.nextConfig, nextConfig: params.request.nextConfig,
}) })
if (!process.env.__NEXT_NO_MIDDLEWARE_URL_NORMALIZE) {
if (rewriteUrl.host === request.nextUrl.host) { if (rewriteUrl.host === request.nextUrl.host) {
rewriteUrl.buildId = buildId || rewriteUrl.buildId rewriteUrl.buildId = buildId || rewriteUrl.buildId
rewriteUrl.flightSearchParameters = rewriteUrl.flightSearchParameters =
flightSearchParameters || rewriteUrl.flightSearchParameters flightSearchParameters || rewriteUrl.flightSearchParameters
response.headers.set('x-middleware-rewrite', String(rewriteUrl)) response.headers.set('x-middleware-rewrite', String(rewriteUrl))
} }
}
/** /**
* When the request is a data request we must show if there was a rewrite * When the request is a data request we must show if there was a rewrite
...@@ -149,12 +151,14 @@ export async function adapter(params: { ...@@ -149,12 +151,14 @@ export async function adapter(params: {
*/ */
response = new Response(response.body, response) response = new Response(response.body, response)
if (!process.env.__NEXT_NO_MIDDLEWARE_URL_NORMALIZE) {
if (redirectURL.host === request.nextUrl.host) { if (redirectURL.host === request.nextUrl.host) {
redirectURL.buildId = buildId || redirectURL.buildId redirectURL.buildId = buildId || redirectURL.buildId
redirectURL.flightSearchParameters = redirectURL.flightSearchParameters =
flightSearchParameters || redirectURL.flightSearchParameters flightSearchParameters || redirectURL.flightSearchParameters
response.headers.set('Location', String(redirectURL)) response.headers.set('Location', String(redirectURL))
} }
}
/** /**
* When the request is a data request we can't use the location header as * When the request is a data request we can't use the location header as
...@@ -179,6 +183,10 @@ export async function adapter(params: { ...@@ -179,6 +183,10 @@ export async function adapter(params: {
export function blockUnallowedResponse( export function blockUnallowedResponse(
promise: Promise<FetchEventResult> promise: Promise<FetchEventResult>
): Promise<FetchEventResult> { ): Promise<FetchEventResult> {
if (process.env.__NEXT_ALLOW_MIDDLEWARE_RESPONSE_BODY) {
return promise
}
return promise.then((result) => { return promise.then((result) => {
if (result.response?.body) { if (result.response?.body) {
console.error( console.error(
......
...@@ -100,7 +100,7 @@ export class NextURL { ...@@ -100,7 +100,7 @@ export class NextURL {
private analyzeUrl() { private analyzeUrl() {
const pathnameInfo = getNextPathnameInfo(this[Internal].url.pathname, { const pathnameInfo = getNextPathnameInfo(this[Internal].url.pathname, {
nextConfig: this[Internal].options.nextConfig, nextConfig: this[Internal].options.nextConfig,
parseData: true, parseData: !process.env.__NEXT_NO_MIDDLEWARE_URL_NORMALIZE,
}) })
this[Internal].domainLocale = detectDomainLocale( this[Internal].domainLocale = detectDomainLocale(
......
import { NextResponse } from 'next/server'
export default function handler(req) {
if (req.nextUrl.pathname.startsWith('/_next/data/missing-id')) {
console.log(`missing-id rewrite: ${req.nextUrl.toString()}`)
return NextResponse.rewrite('https://example.vercel.sh')
}
if (req.nextUrl.pathname === '/middleware-rewrite-with-slash') {
return NextResponse.rewrite(new URL('/another/', req.nextUrl))
}
if (req.nextUrl.pathname === '/middleware-rewrite-without-slash') {
return NextResponse.rewrite(new URL('/another', req.nextUrl))
}
if (req.nextUrl.pathname === '/middleware-redirect-external-with') {
return NextResponse.redirect('https://example.vercel.sh/somewhere/', 307)
}
if (req.nextUrl.pathname === '/middleware-redirect-external-without') {
return NextResponse.redirect('https://example.vercel.sh/somewhere', 307)
}
if (req.nextUrl.pathname.startsWith('/api/test-cookie')) {
const res = NextResponse.next()
res.cookies.set('from-middleware', 1)
return res
}
if (req.nextUrl.pathname === '/middleware-response-body') {
return new Response('hello from middleware', {
headers: {
'x-from-middleware': 'true',
},
})
}
return NextResponse.next()
}
/** @type {import('next').NextConfig} */
const nextConfig = {
experimental: {
skipTrailingSlashRedirect: true,
skipMiddlewareUrlNormalize: true,
allowMiddlewareResponseBody: true,
newNextLinkBehavior: true,
},
async redirects() {
return [
{
source: '/redirect-me',
destination: '/another',
permanent: false,
},
]
},
async rewrites() {
return [
{
source: '/rewrite-me',
destination: '/another',
},
]
},
}
module.exports = nextConfig
import Link from 'next/link'
export default function Page(props) {
return (
<>
<p id="another">another page</p>
<Link href="/" id="to-index">
to index
</Link>
<br />
</>
)
}
import { NextResponse } from 'next/server'
export const config = {
runtime: 'experimental-edge',
}
export default function handler(req) {
console.log('setting cookie in api route')
const res = NextResponse.json({ name: 'API' })
res.cookies.set('hello', 'From API')
return res
}
export default function handler(req, res) {
console.log('setting cookie in api route')
res.setHeader('Set-Cookie', 'hello=From API')
res.status(200).json({ name: 'API' })
}
import Link from 'next/link'
export default function Page(props) {
return (
<>
<p id="blog">blog page</p>
<Link href="/" id="to-index">
to index
</Link>
<br />
</>
)
}
import Link from 'next/link'
export default function Page(props) {
return (
<>
<p id="index">index page</p>
<Link href="/another" id="to-another">
to another
</Link>
<br />
<Link href="/blog/first" id="to-blog-first">
to /blog/first
</Link>
<br />
</>
)
}
import { createNext, FileRef } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import { check, fetchViaHTTP } from 'next-test-utils'
import { join } from 'path'
import webdriver from 'next-webdriver'
describe('skip-trailing-slash-redirect', () => {
let next: NextInstance
beforeAll(async () => {
next = await createNext({
files: new FileRef(join(__dirname, 'app')),
dependencies: {},
})
})
afterAll(() => next.destroy())
it('should allow rewriting invalid buildId correctly', async () => {
const res = await fetchViaHTTP(
next.url,
'/_next/data/missing-id/hello.json',
undefined,
{
headers: {
'x-nextjs-data': '1',
},
}
)
expect(res.status).toBe(200)
expect(await res.text()).toContain('Example Domain')
if (!(global as any).isNextDeploy) {
await check(() => next.cliOutput, /missing-id rewrite/)
expect(next.cliOutput).toContain('/_next/data/missing-id/hello.json')
}
})
it('should allow response body from middleware with flag', async () => {
const res = await fetchViaHTTP(next.url, '/middleware-response-body')
expect(res.status).toBe(200)
expect(res.headers.get('x-from-middleware')).toBe('true')
expect(await res.text()).toBe('hello from middleware')
})
it('should merge cookies from middleware and API routes correctly', async () => {
const res = await fetchViaHTTP(next.url, '/api/test-cookie', undefined, {
redirect: 'manual',
})
expect(res.status).toBe(200)
expect(res.headers.get('set-cookie')).toEqual(
'from-middleware=1; Path=/, hello=From API'
)
})
it('should merge cookies from middleware and edge API routes correctly', async () => {
const res = await fetchViaHTTP(
next.url,
'/api/test-cookie-edge',
undefined,
{
redirect: 'manual',
}
)
expect(res.status).toBe(200)
expect(res.headers.get('set-cookie')).toEqual(
'from-middleware=1; Path=/, hello=From%20API; Path=/'
)
})
if ((global as any).isNextStart) {
it('should not have trailing slash redirects in manifest', async () => {
const routesManifest = JSON.parse(
await next.readFile('.next/routes-manifest.json')
)
expect(
routesManifest.redirects.some((redirect) => {
return (
redirect.statusCode === 308 &&
(redirect.destination === '/:path+' ||
redirect.destination === '/:path+/')
)
})
).toBe(false)
})
}
it('should correct skip URL normalizing in middleware', async () => {
let res = await fetchViaHTTP(
next.url,
'/middleware-rewrite-with-slash',
undefined,
{ redirect: 'manual', headers: { 'x-nextjs-data': '1' } }
)
expect(res.headers.get('x-nextjs-rewrite').endsWith('/another/')).toBe(true)
res = await fetchViaHTTP(
next.url,
'/middleware-rewrite-without-slash',
undefined,
{ redirect: 'manual', headers: { 'x-nextjs-data': '1' } }
)
expect(res.headers.get('x-nextjs-rewrite').endsWith('/another')).toBe(true)
res = await fetchViaHTTP(
next.url,
'/middleware-redirect-external-with',
undefined,
{ redirect: 'manual' }
)
expect(res.status).toBe(307)
expect(res.headers.get('Location')).toBe(
'https://example.vercel.sh/somewhere/'
)
res = await fetchViaHTTP(
next.url,
'/middleware-redirect-external-without',
undefined,
{ redirect: 'manual' }
)
expect(res.status).toBe(307)
expect(res.headers.get('Location')).toBe(
'https://example.vercel.sh/somewhere'
)
})
it('should apply config redirect correctly', async () => {
const res = await fetchViaHTTP(next.url, '/redirect-me', undefined, {
redirect: 'manual',
})
expect(res.status).toBe(307)
expect(new URL(res.headers.get('location'), 'http://n').pathname).toBe(
'/another'
)
})
it('should apply config rewrites correctly', async () => {
const res = await fetchViaHTTP(next.url, '/rewrite-me', undefined, {
redirect: 'manual',
})
expect(res.status).toBe(200)
expect(await res.text()).toContain('another page')
})
it('should not apply trailing slash redirect (with slash)', async () => {
const res = await fetchViaHTTP(next.url, '/another/', undefined, {
redirect: 'manual',
})
expect(res.status).toBe(200)
expect(await res.text()).toContain('another page')
})
it('should not apply trailing slash redirect (without slash)', async () => {
const res = await fetchViaHTTP(next.url, '/another', undefined, {
redirect: 'manual',
})
expect(res.status).toBe(200)
expect(await res.text()).toContain('another page')
})
it('should respond to index correctly', async () => {
const res = await fetchViaHTTP(next.url, '/', undefined, {
redirect: 'manual',
})
expect(res.status).toBe(200)
expect(await res.text()).toContain('index page')
})
it('should respond to dynamic route correctly', async () => {
const res = await fetchViaHTTP(next.url, '/blog/first', undefined, {
redirect: 'manual',
})
expect(res.status).toBe(200)
expect(await res.text()).toContain('blog page')
})
it('should navigate client side correctly', async () => {
const browser = await webdriver(next.url, '/')
expect(await browser.eval('location.pathname')).toBe('/')
await browser.elementByCss('#to-another').click()
await browser.waitForElementByCss('#another')
expect(await browser.eval('location.pathname')).toBe('/another')
await browser.back()
await browser.waitForElementByCss('#index')
expect(await browser.eval('location.pathname')).toBe('/')
await browser.elementByCss('#to-blog-first').click()
await browser.waitForElementByCss('#blog')
expect(await browser.eval('location.pathname')).toBe('/blog/first')
})
})
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