I have a vue/vite website that i'm migrating to Nuxt to improve structure and SEO.
Most pages work fine, but the page that uses jQuery returns a 500 Error, jquery is not defined.
I thought this must be because it's trying to render server-side so I disabled that (so I thought) by adding the following code in nuxt.config.ts:
routeRules: {
'/profile/**': { ssr: false },
'/team/**': { ssr: false },
}
But I still get the following error.
500
jQuery is not defined
at http://localhost:3000/_nuxt/assets/slickgrid/lib/jquery-ui-1.11.3.min.js:6:75
at http://localhost:3000/_nuxt/assets/slickgrid/lib/jquery-ui-1.11.3.min.js:6:84
This is the start of the vue file that is causing the error:
<script>
import {$,jQuery} from 'jquery'
// export for others scripts to use
// window.$ = $;
// window.jQuery = jQuery;
// import '@/assets/jquery.js'
import '@/assets/slickgrid/lib/firebugx.js'
import '@/assets/slickgrid/lib/jquery-ui-1.11.3.min.js'
How can I load these files client-side only? Apologies if this is a dumb question, I might be completely misunderstanding the concept.
As stated here, it's not recommended to use jquery on your Nuxt projects. https://stackoverflow.com/a/66423820/21341655
If you really want to, there are some ways to do it.
npm i jqueryMethod 1
~/plugins/jquery.client.jsMethod 2
~/plugins/jquery.jsMethod 3
https://stackoverflow.com/a/76454524/21341655
Example usage
To use JQuery in a single page, here is the basic example.
~/pages/test.vueTested and it works.
Hope that helps!
Edit: Disabling the SSR in
nuxt.config.tsis optional. These methods will work without applying therouteRulesSlick Grid basic configuration and example