From dbb412437a6e20ee904d92797ec6227ba106136d Mon Sep 17 00:00:00 2001 From: Toru Kobayashi Date: Tue, 9 May 2023 08:14:48 +0900 Subject: [PATCH] fix: better error message with an invalid assetPrefix (#49403) fixes #47641 This adds a friendly error for loading a font error caused by the invalid `assetPrefix` setting. Current: image This PR: image --------- Co-authored-by: JJ Kasper --- .../build/webpack/loaders/next-font-loader/index.ts | 9 +++++++++ .../fixtures/invalid-assertprefix/next.config.js | 3 +++ .../fixtures/invalid-assertprefix/pages/index.js | 10 ++++++++++ test/integration/font-optimization/test/index.test.js | 11 +++++++++++ 4 files changed, 33 insertions(+) create mode 100644 test/integration/font-optimization/fixtures/invalid-assertprefix/next.config.js create mode 100644 test/integration/font-optimization/fixtures/invalid-assertprefix/pages/index.js diff --git a/packages/next/src/build/webpack/loaders/next-font-loader/index.ts b/packages/next/src/build/webpack/loaders/next-font-loader/index.ts index 7bacdbe083..87221932b2 100644 --- a/packages/next/src/build/webpack/loaders/next-font-loader/index.ts +++ b/packages/next/src/build/webpack/loaders/next-font-loader/index.ts @@ -49,6 +49,15 @@ export default async function nextFontLoader(this: any) { postcss: getPostcss, } = this.getOptions() + if (assetPrefix && !/^\/|https?:\/\//.test(assetPrefix)) { + const err = new Error( + 'assetPrefix must start with a leading slash or be an absolute URL(http:// or https://)' + ) + err.name = 'NextFontError' + callback(err) + return + } + /** * Emit font files to .next/static/media as [hash].[ext]. * diff --git a/test/integration/font-optimization/fixtures/invalid-assertprefix/next.config.js b/test/integration/font-optimization/fixtures/invalid-assertprefix/next.config.js new file mode 100644 index 0000000000..0e35ec361f --- /dev/null +++ b/test/integration/font-optimization/fixtures/invalid-assertprefix/next.config.js @@ -0,0 +1,3 @@ +module.exports = { + assetPrefix: '.', +} diff --git a/test/integration/font-optimization/fixtures/invalid-assertprefix/pages/index.js b/test/integration/font-optimization/fixtures/invalid-assertprefix/pages/index.js new file mode 100644 index 0000000000..6c4bf4e7f3 --- /dev/null +++ b/test/integration/font-optimization/fixtures/invalid-assertprefix/pages/index.js @@ -0,0 +1,10 @@ +import React from 'react' +import { Inter } from 'next/font/google' + +const inter = Inter({ subsets: ['latin'] }) + +const Page = () => { + return
Hello
+} + +export default Page diff --git a/test/integration/font-optimization/test/index.test.js b/test/integration/font-optimization/test/index.test.js index d0c58172db..b0c7d804e8 100644 --- a/test/integration/font-optimization/test/index.test.js +++ b/test/integration/font-optimization/test/index.test.js @@ -313,4 +313,15 @@ describe('Font Optimization', () => { ) }) }) + describe('invalid configuration', () => { + it('should show a proper error if assetPrefix starts with .', async () => { + const appDir = join(fixturesDir, 'invalid-assertprefix') + const { stderr } = await nextBuild(appDir, undefined, { + stderr: true, + }) + expect(stderr).toContain( + 'assetPrefix must start with a leading slash or be an absolute URL(http:// or https://)' + ) + }) + }) })