Code Monkey home page Code Monkey logo

Comments (7)

AlexBueckig avatar AlexBueckig commented on September 24, 2024 1

Experienced this issue again, back on 14.1.3. force-dynamic is activated and no other CSP-Errors occur.

Here are some screenshots from the developer tools showing the error and file:

Bildschirmfoto vom 2024-03-11 12-02-04
Bildschirmfoto vom 2024-03-11 12-05-49

from next.js.

AlexBueckig avatar AlexBueckig commented on September 24, 2024

Can confirm. 14.0.4 still works fine for me.

from next.js.

coffeecupjapan avatar coffeecupjapan commented on September 24, 2024

@moloch--
I am not quite sure, since I have not used CSP in my hobby or in work coding, but document says you must use dynamic rendering to add nonces. , and I think app-router is default to cache.
So I think you have to add

export const dynamic = "force-dynamic"

or something to prevent next.js from cacheing in page.tsx in your reproduction codes.

In my case, adding above code fix console.log to warn.

from next.js.

moloch-- avatar moloch-- commented on September 24, 2024

Doesn't seem like I should have to force dynamic rendering for every page just to use CSP, so still seems like a bug?

@AlexBueckig would you be able to link to a working example?

from next.js.

AlexBueckig avatar AlexBueckig commented on September 24, 2024

Adding force-dynamic solved the problem for me. I totally makes sense, now that I'm thinking about it. A new nonce gets created for every request by your middleware function. If you cache a rendered page the nonces in this page are a value from a previous request. But since you create a new csp header for every request, there is a mismatch with these nonces and csp breaks.

For now this seems to be the only option if you want to use nonces.

I don't know your project, but if you aren't using external scripts e.g. tracking or whatever you can just set static csp headers without the need for strict-dynamic and nonces. These versions should be cacheable since they are not dependant on a random generated value.

from next.js.

moloch-- avatar moloch-- commented on September 24, 2024

btw, adding export const dynamic = "force-dynamic" doesn't seem to have any affect for me :-/

from next.js.

moloch-- avatar moloch-- commented on September 24, 2024

Okay for anyone else that sees this, I figured it out. I had "use client" in my layout.tsx which means the nonces are never inserted even if you use force-dynamic

from next.js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.