Skip to content

Commit

Permalink
Merge branch 'canary' into i18n/client-locale-domains
Browse files Browse the repository at this point in the history
  • Loading branch information
kodiakhq[bot] authored Feb 11, 2021
2 parents b24a893 + 5febe21 commit 59779b5
Show file tree
Hide file tree
Showing 12 changed files with 152 additions and 15 deletions.
6 changes: 5 additions & 1 deletion packages/next/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,11 @@ export default async function build(
path.relative(
dir,
path.join(
path.dirname(require.resolve('@ampproject/toolbox-optimizer')),
path.dirname(
require.resolve(
'next/dist/compiled/@ampproject/toolbox-optimizer'
)
),
'**/*'
)
)
Expand Down
2 changes: 1 addition & 1 deletion packages/next/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,7 @@ export default async function getBaseWebpackConfig(
: [
// When the 'serverless' target is used all node_modules will be compiled into the output bundles
// So that the 'serverless' bundles have 0 runtime dependencies
'@ampproject/toolbox-optimizer', // except this one
'next/dist/compiled/@ampproject/toolbox-optimizer', // except this one

// Mark this as external if not enabled so it doesn't cause a
// webpack error from being missing
Expand Down
28 changes: 21 additions & 7 deletions packages/next/next-server/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ export default class Server {
this.nextConfig = loadConfig(phase, this.dir, conf)
this.distDir = join(this.dir, this.nextConfig.distDir)
this.publicDir = join(this.dir, CLIENT_PUBLIC_FILES_PATH)
this.hasStaticDir = fs.existsSync(join(this.dir, 'static'))
this.hasStaticDir = !minimalMode && fs.existsSync(join(this.dir, 'static'))

// Only serverRuntimeConfig needs the default
// publicRuntimeConfig gets it's default in client/index.js
Expand Down Expand Up @@ -568,6 +568,9 @@ export default class Server {
try {
return await this.run(req, res, parsedUrl)
} catch (err) {
if (this.minimalMode) {
throw err
}
this.logError(err)
res.statusCode = 500
res.end('Internal Server Error')
Expand Down Expand Up @@ -1836,14 +1839,19 @@ export default class Server {
}
}
} catch (err) {
this.logError(err)

if (err && err.code === 'DECODE_FAILED') {
this.logError(err)
res.statusCode = 400
return await this.renderErrorToHTML(err, req, res, pathname, query)
}
res.statusCode = 500
return await this.renderErrorToHTML(err, req, res, pathname, query)
const html = await this.renderErrorToHTML(err, req, res, pathname, query)

if (this.minimalMode) {
throw err
}
this.logError(err)
return html
}
res.statusCode = 404
return await this.renderErrorToHTML(null, req, res, pathname, query)
Expand All @@ -1864,6 +1872,10 @@ export default class Server {
)
}
const html = await this.renderErrorToHTML(err, req, res, pathname, query)

if (this.minimalMode && res.statusCode === 500) {
throw err
}
if (html === null) {
return
}
Expand Down Expand Up @@ -2008,9 +2020,11 @@ export default class Server {
}

let nextFilesStatic: string[] = []
nextFilesStatic = recursiveReadDirSync(
join(this.distDir, 'static')
).map((f) => join('.', relative(this.dir, this.distDir), 'static', f))
nextFilesStatic = !this.minimalMode
? recursiveReadDirSync(join(this.distDir, 'static')).map((f) =>
join('.', relative(this.dir, this.distDir), 'static', f)
)
: []

return (this._validFilesystemPathSet = new Set<string>([
...nextFilesStatic,
Expand Down
2 changes: 1 addition & 1 deletion packages/next/next-server/server/optimize-amp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export default async function optimize(
): Promise<string> {
let AmpOptimizer
try {
AmpOptimizer = require('@ampproject/toolbox-optimizer')
AmpOptimizer = require('next/dist/compiled/@ampproject/toolbox-optimizer')
} catch (_) {
return html
}
Expand Down
2 changes: 1 addition & 1 deletion packages/next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
]
},
"dependencies": {
"@ampproject/toolbox-optimizer": "2.7.1-alpha.0",
"@babel/runtime": "7.12.5",
"@hapi/accept": "5.0.1",
"@next/env": "10.0.7-canary.6",
Expand Down Expand Up @@ -120,6 +119,7 @@
}
},
"devDependencies": {
"@ampproject/toolbox-optimizer": "2.7.1-alpha.0",
"@babel/code-frame": "7.12.11",
"@babel/core": "7.12.10",
"@babel/plugin-proposal-class-properties": "7.12.1",
Expand Down
10 changes: 7 additions & 3 deletions packages/next/taskfile-ncc.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ module.exports = function (task) {
options.externals = { ...options.externals }
delete options.externals[options.packageName]
}
let precompiled = options.precompiled !== false
delete options.precompiled

return ncc(join(__dirname, file.dir, file.base), {
filename: file.base,
minify: options.minify === false ? false : true,
Expand All @@ -39,7 +42,8 @@ module.exports = function (task) {
this,
options.packageName,
file.base,
options.bundleName
options.bundleName,
precompiled
)
}

Expand All @@ -51,13 +55,13 @@ module.exports = function (task) {
// This function writes a minimal `package.json` file for a compiled package.
// It defines `name`, `main`, `author`, and `license`. It also defines `types`.
// n.b. types intended for development usage only.
function writePackageManifest(packageName, main, bundleName) {
function writePackageManifest(packageName, main, bundleName, precompiled) {
const packagePath = bundleRequire.resolve(packageName + '/package.json')
let { name, author, license } = require(packagePath)

const compiledPackagePath = join(
__dirname,
`compiled/${bundleName || packageName}`
`${!precompiled ? 'dist/' : ''}compiled/${bundleName || packageName}`
)

const potentialLicensePath = join(dirname(packagePath), './LICENSE')
Expand Down
19 changes: 19 additions & 0 deletions packages/next/taskfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,22 @@ export async function ncc_amphtml_validator(task, opts) {
.target('compiled/amphtml-validator')
}
// eslint-disable-next-line camelcase
externals['@ampproject/toolbox-optimizer'] =
'next/dist/compiled/@ampproject/toolbox-optimizer'
export async function ncc_amp_optimizer(task, opts) {
await task
.source(
opts.src ||
relative(__dirname, require.resolve('@ampproject/toolbox-optimizer'))
)
.ncc({
externals,
precompiled: false,
packageName: '@ampproject/toolbox-optimizer',
})
.target('dist/compiled/@ampproject/toolbox-optimizer')
}
// eslint-disable-next-line camelcase
externals['arg'] = 'distcompiled/arg'
export async function ncc_arg(task, opts) {
await task
Expand Down Expand Up @@ -757,6 +773,9 @@ export async function compile(task, opts) {
'client',
'telemetry',
'nextserver',
// we compile this each time so that fresh runtime data is pulled
// before each publish
'ncc_amp_optimizer',
],
opts
)
Expand Down
9 changes: 9 additions & 0 deletions test/.stats-app/pages/amp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { useAmp } from 'next/amp'

export const config = {
amp: 'hybrid',
}

export default function Amp(props) {
return useAmp() ? 'AMP mode' : 'normal mode'
}
14 changes: 14 additions & 0 deletions test/integration/required-server-files/pages/errors/gip.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function Page(props) {
return <p>here comes an error</p>
}

Page.getInitialProps = ({ query }) => {
if (query.crash) {
throw new Error('gip hit an oops')
}
return {
hello: 'world',
}
}

export default Page
28 changes: 28 additions & 0 deletions test/integration/required-server-files/pages/errors/gsp/[post].js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { useRouter } from 'next/router'

function Page(props) {
if (useRouter().isFallback) {
return <p>loading...</p>
}
return <p>here comes an error</p>
}

export const getStaticPaths = () => {
return {
paths: [],
fallback: true,
}
}

export const getStaticProps = ({ params }) => {
if (params.post === 'crash') {
throw new Error('gsp hit an oops')
}
return {
props: {
hello: 'world',
},
}
}

export default Page
16 changes: 16 additions & 0 deletions test/integration/required-server-files/pages/errors/gssp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function Page(props) {
return <p>here comes an error</p>
}

export const getServerSideProps = ({ query }) => {
if (query.crash) {
throw new Error('gssp hit an oops')
}
return {
props: {
hello: 'world',
},
}
}

export default Page
31 changes: 30 additions & 1 deletion test/integration/required-server-files/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ let nextApp
let appPort
let buildId
let requiredFilesManifest
let errors = []

describe('Required Server Files', () => {
beforeAll(async () => {
Expand Down Expand Up @@ -58,7 +59,8 @@ describe('Required Server Files', () => {
try {
await nextApp.getRequestHandler()(req, res)
} catch (err) {
console.error(err)
console.error('top-level', err)
errors.push(err)
res.statusCode = 500
res.end('error')
}
Expand Down Expand Up @@ -419,4 +421,31 @@ describe('Required Server Files', () => {
path: ['hello', 'world'],
})
})

it('should bubble error correctly for gip page', async () => {
errors = []
const res = await fetchViaHTTP(appPort, '/errors/gip', { crash: '1' })
expect(res.status).toBe(500)
expect(await res.text()).toBe('error')
expect(errors.length).toBe(1)
expect(errors[0].message).toContain('gip hit an oops')
})

it('should bubble error correctly for gssp page', async () => {
errors = []
const res = await fetchViaHTTP(appPort, '/errors/gssp', { crash: '1' })
expect(res.status).toBe(500)
expect(await res.text()).toBe('error')
expect(errors.length).toBe(1)
expect(errors[0].message).toContain('gssp hit an oops')
})

it('should bubble error correctly for gsp page', async () => {
errors = []
const res = await fetchViaHTTP(appPort, '/errors/gsp/crash')
expect(res.status).toBe(500)
expect(await res.text()).toBe('error')
expect(errors.length).toBe(1)
expect(errors[0].message).toContain('gsp hit an oops')
})
})

0 comments on commit 59779b5

Please sign in to comment.