question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot use prerendering if config.kit.csp.mode === "nonce"

See original GitHub issue

Describe the bug

I keep getting the issue of Cannot use prerendering if config.kit.csp.mode === "nonce" when doing an npm build with sveltekit but not when in npm dev environment…

I have noticed this now with the most recent sveltekit version, as I am using : next in the package.json.

In addition, removing or setting the:

kit: 
{ 
csp: {
mode: auto
}
}

causes the further issue of Cannot use prerendering if page template contains %svelte.nonce%

Reproduction

package.json

{
	"name": "my-app",
	"version": "0.5.0",
	"engines": {
		"npm": "8.3.0",
		"node": "14.17.4"
	},
	"scripts": {
		"dev": "svelte-kit dev --host --port 3050",
		"build": "svelte-kit build",
		"start": "node my-server.js",
		"preview": "svelte-kit preview --host --port 5000",
		"check": "svelte-check --tsconfig ./tsconfig.json",
		"check:watch": "svelte-check --tsconfig ./tsconfig.json --watch",
		"lint": "prettier --ignore-path .gitignore --check --plugin-search-dir=. . && eslint --ignore-path .gitignore .",
		"format": "prettier --ignore-path .gitignore --write --plugin-search-dir=. ."
	},
	"devDependencies": {
		"@lukeed/uuid": "^2.0.0",
		"@sveltejs/adapter-node": "next",
		"@sveltejs/adapter-vercel": "next",
		"@sveltejs/kit": "next",
		"@types/cookie": "^0.4.1",
		"@typescript-eslint/eslint-plugin": "^4.31.1",
		"@typescript-eslint/parser": "^4.31.1",
		"cookie": "^0.4.1",
		"eslint": "^7.32.0",
		"eslint-config-prettier": "^8.3.0",
		"eslint-plugin-svelte3": "^3.2.1",
		"node-fetch": "^3.1.0",
		"prettier": "^2.4.1",
		"prettier-plugin-svelte": "^2.4.0",
		"svelte": "^3.34.0",
		"svelte-check": "^2.2.6",
		"svelte-preprocess": "^4.9.4",
		"svelte-seo": "^1.3.1",
		"tslib": "^2.3.1",
		"typescript": "^4.4.3"
	},
	"type": "module",
	"dependencies": {
		"colorthief": "^2.3.2",
		"cookie": "^0.4.1",
		"express": "^4.17.3",
		"express-sslify": "^1.2.0",
		"firebase": "^9.6.0",
		"graphql": "^15.7.2",
		"graphql-request": "^3.6.1",
		"heroku-ssl-redirect": "^0.1.1",
		"ioredis": "^4.28.2",
		"node-fetch": "^3.1.0",
		"svelte": "^3.34.0",
		"svelte-content-loader": "^1.1.3",
		"svelte-seo": "^1.3.1"
	}
}

sveltkit.config.js

// import vercel from '@sveltejs/adapter-vercel';
import node from '@sveltejs/adapter-node';
import preprocess from 'svelte-preprocess';

/** @type {import('@sveltejs/kit').Config} */
const config = {

	// ... ℹ consult https://github.com/sveltejs/svelte-preprocess
	// ... ℹ for more information about preprocessors
	preprocess: preprocess(),

	kit: {
		// ... ℹ adding vercel support ...
		// adapter: vercel()

		// ... ℹ NODE-JS deployment Environment ...
		adapter: node(),

    // ... ℹ SVELTEKIT-MIDDLEWARE-SUPPORT;
    // vite: {
    //   plugins: [myPlugin],
    // },

    // ... ℹ CSP Support - [PROD / HEROKU ONLY]
    // ... https://developers.google.com/tag-platform/tag-manager/web/csp [GOOGLE-CSP]
    // ... https://yandex.com/support/metrica/code/install-counter-csp.html [YANDEX-CSP]
    csp: {
      // mode: 'nonce',
      directives: {
        // 'default-src': ['*'],        // ... ultimate-default-fallback;
        // 'script-src-elem': ['self', 
        //                     'https://betarena-rv-6b382.firebaseio.com/'],
        'script-src': ['self', 
                       'strict-dynamic',
                      'unsafe-inline', 
                      'https://www.googletagmanager.com',
                      'https://www.google-analytics.com',
                      'https://ssl.google-analytics.com',
                      'https://betarena-rv-6b382.firebaseio.com/',
                      '*.firebaseio.com',
                      'https://mc.yandex.ru',
                      'https://yastatic.net'], 
        'style-src': ['self',
                      'unsafe-inline',
                      'https://fonts.googleapis.com'],
        'object-src': ['none'],
        'base-uri': ['none'],
        // 'img-src': ['http://*', 
        //             'https://*'],
        // 'connect-src': ['https://get.geojs.io', 
        //                 'https://betarena.hasura.app', 
        //                 'https://betarena-rv-6b382.firebaseio.com/']
      }
    }
	}
};

export default config;

Logs

Error: Cannot use prerendering if config.kit.csp.mode === "nonce"
    at render_response (file:///C:/Users/migue/OneDrive/Projects/Betarena/scores-platform/.svelte-kit/output/server/index.js:777:13)
    at resolve (file:///C:/Users/migue/OneDrive/Projects/Betarena/scores-platform/.svelte-kit/output/server/index.js:1824:24)
    at async Object.handle (file:///C:/Users/migue/OneDrive/Projects/Betarena/scores-platform/.svelte-kit/output/server/chunks/hooks-5e34d30f.js:9:20)
    at async respond (file:///C:/Users/migue/OneDrive/Projects/Betarena/scores-platform/.svelte-kit/output/server/index.js:1814:22)
    at async fetch (file:///C:/Users/migue/OneDrive/Projects/Betarena/scores-platform/.svelte-kit/output/server/index.js:1191:22)
    at async load (file:///C:/Users/migue/OneDrive/Projects/Betarena/scores-platform/.svelte-kit/output/server/entries/pages/__layout.svelte.js:476:27)
    at async load_node (file:///C:/Users/migue/OneDrive/Projects/Betarena/scores-platform/.svelte-kit/output/server/index.js:1281:14)
    at async respond_with_error (file:///C:/Users/migue/OneDrive/Projects/Betarena/scores-platform/.svelte-kit/output/server/index.js:1415:27)
    at async respond (file:///C:/Users/migue/OneDrive/Projects/Betarena/scores-platform/.svelte-kit/output/server/index.js:1919:14)
    at async fetch (file:///C:/Users/migue/OneDrive/Projects/Betarena/scores-platform/.svelte-kit/output/server/index.js:1191:22)

System Info

System:
    OS: Windows 10 10.0.19044
    CPU: (4) x64 Intel(R) Core(TM) i7-7500U CPU @ 2.70GHz
    Memory: 2.38 GB / 15.90 GB
  Binaries:
    Node: 16.13.2 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.4 - C:\Program Files (x86)\Yarn\bin\yarn.CMD
    npm: 8.3.0 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Spartan (44.19041.1266.0), Chromium (99.0.1150.46)
    Internet Explorer: 11.0.19041.1566

Severity

blocking an upgrade

Additional Information

Only issue occurs in npm build and not npm run dev

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
benmccanncommented, Mar 24, 2022
0reactions
migbashcommented, Mar 23, 2022

@mrkishi I had no build time errors before, just now they started, and the %svelte.nonce% no longer works for scripts such as:

app.html

<link nonce="%svelte.nonce%" rel="preconnect" href="https://fonts.googleapis.com">
		<link nonce="%svelte.nonce%" rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
		<link nonce="%svelte.nonce%" href="https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,100;0,300;0,400;0,500;0,700;0,900;1,100;1,300;1,400;1,500;1,700;1,900&display=swap" rel="stylesheet">

How can I use hashes instead, as it mentions I can use them for prerendered pages ?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Content Security Policy support · Issue #93 · sveltejs/kit - GitHub
Since prerendering is something supported by all adapters, not just adapter-static, I lean towards using hashes rather than nonces. One possible idea would...
Read more >
Configuration • Docs • SvelteKit
When mode is 'auto' , SvelteKit will use nonces for dynamically rendered pages and hashes for prerendered pages. Using nonces with prerendered pages...
Read more >
SvelteKit async load styles with strict CSP (no unsafe-inline ...
nonce is a script tag attribute. It seems like I should re-write the link tag without the onload and then set an event...
Read more >
Connect your content to SvelteKit - Sanity.io
Below, you'll find a simple boilerplate as an embedded CodeSandbox project. If ... new Error('Cannot use prerendering if config.kit.csp.mode === \"nonce\"') ...
Read more >
Content-Security-Policy - HTTP - MDN Web Docs
Restricts the set of plugins that can be embedded into a document by limiting the types of resources which can be loaded. referrer...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found