CSP: $ is not defined error when PF, Springboot, Joinfaces
Describe the bug
I'm getting error in the console: $ is not defined.


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
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
I fixed your pom.xml and your application.yaml and its all working for me now. Also had to fix your SpringBootApplication.
I also made some optimizations.
@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.
2 things.
- run
mvn clean spring-boot:run - 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.
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.
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>
You can also explicitly load the bundled jQuery from PrimeFaces if you are not using any components.
@melloware Thank you - adding some pf component (styled as display: none is working correctly.
@jepsar I mentioned adding jQuery as workaround in the ticket.
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.
I tried all three:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script><h:outputScript library="primefaces" name="jquery/jquery.js" target="head" />- '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
Ahh yeah you need more than just jquery included from PF.