Nuxt/kit: when creating a module, how can I inject a script into the body
Hi there. I am currently creating a nuxt module. One of the things I need to do is, I have a minified .js file I want to inject into the body. But only on client side. I tried
This does include the script directly, so also in SSR. When adding something like
if (typeof window !== 'undefined')
to only inject it on the client-side, it never gets injected since the src/module.ts
seems to only run once on start.
I am sure that addTemplate
is not the right method.
Do you know any method to add a file into the public
folder?14 Replies
You should reference how the color module does it
GitHub
color-mode/module.ts at master · nuxt-modules/color-mode
Dark and Light mode with auto detection made easy with Nuxt 🌗 - color-mode/module.ts at master · nuxt-modules/color-mode
GitHub
color-mode/nitro-plugin.ts at master · nuxt-modules/color-mode
Dark and Light mode with auto detection made easy with Nuxt 🌗 - color-mode/nitro-plugin.ts at master · nuxt-modules/color-mode
probably the easiest
Thanks @harlan , I see that this would inject the script as inline, but not actually "upload" or add a JS file to the
public
folder of the modules' user, right?
The usecase is that I have a .min.js script coming from a 3rd party, and this needs to be injected into the public
folder of a user.maybe something like
?
That looks great, though it doesn't actually add those files to the
playground/public
folder. I've worked a lot with nuxt, but not yet created a module, so maybe I am overlooking somthing 😄
I saw someone did it with just fs.writeFile, not sure about if that's the best approach.
https://github.com/nuxt/nuxt/discussions/17807#discussioncomment-2339732
https://github.com/florian-lefebvre/portfolio/blob/c513428dea912a19ffb684b8b571b08b8882158c/modules/sitemap.ts#L54so this code basically adds a new public dir for nitro to serve static assets from
if you hit the path it should serve the file
using fs is fine too
if you don't need it in dev
I now have implemented it like so:
and the nitro plugin
which works perfectly well. Thanks again 🙂
looks good, only issue is that issues will have an untracked git file with this setup
if that's not an issue then all good
Yeah that's right. But since the script is only needed in prod and also I plan to change the content inside of the script based on user config (since it contains a hardcoded url I want users to override), I think it's perfectly fine for the file not to be in git, but to be generated on the fly in build.
A small follow-up, maybe you've stumbled on this. I have now released the package on a @Next tag. It is on npm with
nuxt-cloudflare-analytics@next
.
When testing this module in the playground, everything works fine. Installing this module in another real nuxt app, I get the following error on npm run build:
I've tried to implement it like i the color-module:
https://github.com/hamlogic/nuxt-cloudflare-analytics/blob/next/src/module.ts
https://github.com/hamlogic/nuxt-cloudflare-analytics/blob/next/src/runtime/nitro-plugin.ts
Do you have an idea on why this seems to fail? Thanks in advance! @harlan
Solved it, nevermind 😄Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
I forgot to include these 3 lines
and they are somehow required
Here is the repo https://github.com/hamlogic/nuxt-cloudflare-analytics/
GitHub
GitHub - hamlogic/nuxt-cloudflare-analytics: Add Cloudflare Web Ana...
Add Cloudflare Web Analytics to your Nuxt Project. Contribute to hamlogic/nuxt-cloudflare-analytics development by creating an account on GitHub.