未验证 提交 4e3b5ae2 编写于 作者: J JJ Kasper 提交者: GitHub

Update routes-manifest and add tests (#9361)

* Test custom-routes serverless

* Make sure we add default statusCode to routes-manifest

* Update routes-manifest

* Add DEFAULT_REDIRECT_STATUS constant
上级 7c83a123
......@@ -21,6 +21,7 @@ import {
SERVER_DIRECTORY,
SERVERLESS_DIRECTORY,
ROUTES_MANIFEST,
DEFAULT_REDIRECT_STATUS,
} from '../next-server/lib/constants'
import {
getRouteRegex,
......@@ -619,13 +620,28 @@ export default async function build(dir: string, conf = null): Promise<void> {
await fsWriteFile(manifestPath, JSON.stringify(pagesManifest), 'utf8')
}
const buildCustomRoute = (r: { source: string }) => {
const buildCustomRoute = (
r: {
source: string
statusCode?: number
},
isRedirect = false
) => {
const keys: any[] = []
const routeRegex = pathToRegexp(r.source, keys, {
strict: true,
sensitive: false,
})
return {
...r,
regex: pathToRegexp(r.source, [], {
strict: true,
sensitive: false,
}).source,
...(isRedirect
? {
statusCode: r.statusCode || DEFAULT_REDIRECT_STATUS,
}
: {}),
regex: routeRegex.source,
regexKeys: keys.map(k => k.name),
}
}
......@@ -633,7 +649,7 @@ export default async function build(dir: string, conf = null): Promise<void> {
path.join(distDir, ROUTES_MANIFEST),
JSON.stringify({
version: 1,
redirects: redirects.map(r => buildCustomRoute(r)),
redirects: redirects.map(r => buildCustomRoute(r, true)),
rewrites: rewrites.map(r => buildCustomRoute(r)),
dynamicRoutes: getSortedRoutes(dynamicRoutes).map(page => ({
page,
......
......@@ -29,3 +29,4 @@ export const IS_BUNDLED_PAGE_REGEX = /^static[/\\][^/\\]+[/\\]pages.*\.js$/
// matches static/<buildid>/pages/:page*.js
export const ROUTE_NAME_REGEX = /^static[/\\][^/\\]+[/\\]pages[/\\](.*)\.js$/
export const SERVERLESS_ROUTE_NAME_REGEX = /^pages[/\\](.*)\.js$/
export const DEFAULT_REDIRECT_STATUS = 307
......@@ -3,7 +3,7 @@ import fs from 'fs'
import { IncomingMessage, ServerResponse } from 'http'
import { join, resolve, sep } from 'path'
import { parse as parseQs, ParsedUrlQuery } from 'querystring'
import { parse as parseUrl, UrlWithParsedQuery } from 'url'
import { parse as parseUrl, format as formatUrl, UrlWithParsedQuery } from 'url'
import { withCoalescedInvoke } from '../../lib/coalesced-function'
import {
......@@ -16,6 +16,7 @@ import {
SERVER_DIRECTORY,
SERVERLESS_DIRECTORY,
ROUTES_MANIFEST,
DEFAULT_REDIRECT_STATUS,
} from '../lib/constants'
import {
getRouteMatcher,
......@@ -415,7 +416,7 @@ export default class Server {
if (r.type === 'redirect') {
res.setHeader('Location', newUrl)
res.statusCode = statusCode || 307
res.statusCode = statusCode || DEFAULT_REDIRECT_STATUS
res.end()
return
}
......@@ -692,6 +693,14 @@ export default class Server {
if (!isSpr) {
// handle serverless
if (isLikeServerless) {
const curUrl = parseUrl(req.url!, true)
req.url = formatUrl({
...curUrl,
query: {
...curUrl.query,
...query,
},
})
return result.Component.renderReqToHTML(req, res)
}
......
module.exports = {
// target: 'serverless',
experimental: {
async rewrites () {
return [
......
/* eslint-env jest */
/* global jasmine */
import url from 'url'
import fs from 'fs-extra'
import { join } from 'path'
import webdriver from 'next-webdriver'
import {
......@@ -18,6 +19,8 @@ import {
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 2
let appDir = join(__dirname, '..')
const nextConfigPath = join(appDir, 'next.config.js')
let nextConfigContent
let appPort
let app
......@@ -127,4 +130,24 @@ describe('Custom routes', () => {
afterAll(() => killApp(app))
runTests()
})
describe('serverless mode', () => {
beforeAll(async () => {
nextConfigContent = await fs.readFile(nextConfigPath, 'utf8')
await fs.writeFile(
nextConfigPath,
nextConfigContent.replace(/\/\/ target/, 'target'),
'utf8'
)
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(async () => {
await killApp(app)
await fs.writeFile(nextConfigPath, nextConfigContent, 'utf8')
})
runTests()
})
})
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册