1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00

Use correct default for query (#5851)

This commit is contained in:
Tim Neutkens 2018-12-10 23:40:26 +01:00 committed by GitHub
parent 6b7864e57e
commit 93424b64a9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 13 additions and 4 deletions

View file

@ -205,7 +205,7 @@ export default class Server {
return sendHTML(req, res, html, {generateEtags})
}
public async render (req: IncomingMessage, res: ServerResponse, pathname: string, query: ParsedUrlQuery, parsedUrl: UrlWithParsedQuery): Promise<void> {
public async render (req: IncomingMessage, res: ServerResponse, pathname: string, query: ParsedUrlQuery = {}, parsedUrl?: UrlWithParsedQuery): Promise<void> {
const url: any = req.url
if (isInternalUrl(url)) {
return this.handleRequest(req, res, parsedUrl)
@ -227,7 +227,7 @@ export default class Server {
return this.sendHTML(req, res, html)
}
public async renderToHTML (req: IncomingMessage, res: ServerResponse, pathname: string, query: ParsedUrlQuery): Promise<string|null> {
public async renderToHTML (req: IncomingMessage, res: ServerResponse, pathname: string, query: ParsedUrlQuery = {}): Promise<string|null> {
try {
// To make sure the try/catch is executed
const html = await renderToHTML(req, res, pathname, query, this.renderOpts)
@ -243,13 +243,13 @@ export default class Server {
}
}
public async renderError (err: Error|null, req: IncomingMessage, res: ServerResponse, pathname: string, query: ParsedUrlQuery): Promise<void> {
public async renderError (err: Error|null, req: IncomingMessage, res: ServerResponse, pathname: string, query: ParsedUrlQuery = {}): Promise<void> {
res.setHeader('Cache-Control', 'no-cache, no-store, max-age=0, must-revalidate')
const html = await this.renderErrorToHTML(err, req, res, pathname, query)
return this.sendHTML(req, res, html)
}
public async renderErrorToHTML (err: Error|null, req: IncomingMessage, res: ServerResponse, pathname: string, query: ParsedUrlQuery) {
public async renderErrorToHTML (err: Error|null, req: IncomingMessage, res: ServerResponse, pathname: string, query: ParsedUrlQuery = {}) {
return renderErrorToHTML(err, req, res, pathname, query, this.renderOpts)
}

View file

@ -0,0 +1 @@
export default () => 'test'

View file

@ -10,6 +10,10 @@ const handleNextRequests = app.getRequestHandler()
app.prepare().then(() => {
const server = new http.Server((req, res) => {
if (req.url === '/no-query') {
return app.render(req, res, '/no-query')
}
if (/setAssetPrefix/.test(req.url)) {
app.setAssetPrefix(`http://127.0.0.1:${port}`)
} else if (/setEmptyAssetPrefix/.test(req.url)) {

View file

@ -37,6 +37,10 @@ describe('Custom Server', () => {
beforeAll(() => startServer())
afterAll(() => killApp(server))
it('should handle render with undefined query', async () => {
expect(await renderViaHTTP(appPort, '/no-query')).toMatch(/"query":/)
})
it('should set the assetPrefix dynamically', async () => {
const normalUsage = await renderViaHTTP(appPort, '/asset')
expect(normalUsage).not.toMatch(/127\.0\.0\.1/)