primefaces icon indicating copy to clipboard operation
primefaces copied to clipboard

CSP: $ is not defined error when PF, Springboot, Joinfaces

Open sasynkamil opened this issue 4 years ago • 10 comments

Describe the bug

I'm getting error in the console: $ is not defined.

image

image

Reproducer

It is plain PrimeFaces & Springboot project (attached):

  • Java EE 11
  • PrimeFaces 11.0.0
  • SpringBoot 2.7.1
  • JoinFaces 4.7.1
  • MyFaces 2.3.10

When CSP is disabled, browser console is without any error. When CSP is enabled in application.yaml, error is thrown.

Work around can be adding jquery to the page:

<h:head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</h:head>

Build the app and run as SpringBoot by: java -jar primefaces-spring-0.0.1-SNAPSHOT.jar Served on: http://localhost:8080

primefaces-spring.zip


Note: I tried to test this setup also with PF master-SNAPSHOT, but then apps is not even runnable - getting during start:

2022-07-08 11:56:31.188 ERROR 13000 --- [           main] o.a.m.webapp.AbstractFacesInitializer    : An error occured while initializing MyFaces: null

java.lang.NullPointerException: null
        at io.github.classgraph.ClassGraphClassLoader.findClass(ClassGraphClassLoader.java:143) ~[classgraph-4.8.147.jar!/:4.8.147]

Expected behavior

No error in the console.

PrimeFaces edition

Community

PrimeFaces version

11.0.0

Theme

Saga

JSF implementation

MyFaces

JSF version

2.3

Browser(s)

No response

sasynkamil avatar Jul 08 '22 09:07 sasynkamil

I fixed your pom.xml and your application.yaml and its all working for me now. Also had to fix your SpringBootApplication.

pf-8973.zip

I also made some optimizations.

melloware avatar Jul 08 '22 11:07 melloware

@melloware I tried to run project you attached, but got no main manifest attribute when I called java -jar primefaces-spring-0.0.1-SNAPSHOT.jar. So I added back the spring-boot-maven-plugin. Now I'm getting 403 Forbidden in the browser.

sasynkamil avatar Jul 08 '22 12:07 sasynkamil

2 things.

  1. run mvn clean spring-boot:run
  2. navigate to http://localhost:8080/test.xhtml you will see its all working including CSP.

i removed your root mapping to "/" that is why you are getting a forbidden that is NOT how you map that.

melloware avatar Jul 08 '22 12:07 melloware

Ok, I see now. But you also added to the application.yml:

 move-scripts-to-bottom: true

And when this is set to the false, the error $ is not defined is back in the console. Unfortunately we have quite a lot of JS scripts in our production app and it stops functional when we set this property to true.

sasynkamil avatar Jul 08 '22 12:07 sasynkamil

let me look. Move Scripts to bottom should never break your app. I have a feeling you have something wrong with your app or your JS code is that is breaking it? I have been using MOVE TO BOTTOM for years now on large and small apps without issue?

Anyway I fixed it again: pf-8973-2.zip

Your issue with $ missing is because on your page you had not a single PrimeFaces component so PF did not load jQuery. As soon as I put a single component on your page it works. For example all I did was add p:spinner

        <h:form id="frmTest">
            <p:spinner />
        </h:form>

melloware avatar Jul 08 '22 13:07 melloware

You can also explicitly load the bundled jQuery from PrimeFaces if you are not using any components.

jepsar avatar Jul 08 '22 13:07 jepsar

@melloware Thank you - adding some pf component (styled as display: none is working correctly.

@jepsar I mentioned adding jQuery as workaround in the ticket.

sasynkamil avatar Jul 08 '22 14:07 sasynkamil

You could have also just done this instead of including an invisible component...

<h:outputScript library="primefaces" name="jquery/jquery.js" target="head" />

That is what I think @jepsar meant.

melloware avatar Jul 08 '22 14:07 melloware

I tried all three:

  1. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  2. <h:outputScript library="primefaces" name="jquery/jquery.js" target="head" />
  3. 'hidden pf component'

In test project all three are working, but just 1. and 3. are working for me in production app. The 2. is throwing:

Uncaught ReferenceError: PrimeFaces is not defined

sasynkamil avatar Jul 08 '22 15:07 sasynkamil

Ahh yeah you need more than just jquery included from PF.

melloware avatar Jul 08 '22 15:07 melloware