12

While I wrote this answer for StackOverflow I came across the problem that I wanted to execute some code exactly once in Nuxt.js when the server is started (or while the server is starting).

I thought about writing a module or a plugin, but I did not succeed, since both were executed every time I contacted the server, i.e., every time I made a call to localhost:3000/myPage. Probably I am missing the right module hook or some plugin setting?

Since I could not find any information on this problem in the official documentation, I now came up with a quite ugly solution: I wrote a serverMiddleware that executes some code (the code that should be run only once) and then returns an empty handler - which now adds (little) unnecessary overhead to every request.

My question: What would have been the best way to execute some code exactly once on a Nuxt.js server during/after startup, but not during building?


Minimal ugly working example:

nuxt.config.js:

export default {
  ...
  serverMiddleware: [ "~/serverMiddleware/run-once.js" ]
  ...
}

~/serverMiddleware/run-once.js:

console.log("Yay, I only run once when the server is started!")
// Since we are a serverMiddleware, we have to return a handler, even if this it does nothing
// I think this is really ugly...
export default function (req, res, next) {
  next()
}

PS: I saw questions such as Is it possible for Nuxt JS plugins to only run once? but it seems to not answer my question since the vue-renderer:ssr:prepareContext nuxt hook also executes every time I send a request to the server.

Also, a module such as the following does not work since it is also executed during nuxt build and not only on nuxt dev or nuxt start:

export default function MyModuleThatShouldOnlyRunOnce (_moduleOptions) {
  console.log("I run on 'nuxt dev' and 'nuxt start', but sadly also on 'nuxt build' :(");
}
Markus Weninger
  • 11,931
  • 7
  • 64
  • 137
  • tried hooks? https://nuxtjs.org/docs/2.x/internals-glossary/internals-builder – bill.gates Apr 03 '21 at 14:00
  • @Ifaruki Yes, but it seems that I have not found the right hook yet. I do not want to run code during building, but when the server is being started. – Markus Weninger Apr 03 '21 at 14:01
  • build:before ? this hook? – bill.gates Apr 03 '21 at 14:02
  • @Ifaruki No, since I do _not_ want to execute code during `nuxt build` but only on `nuxt start` or `nuxt dev`. `"build:before"`, as the name suggests, is executed before building. – Markus Weninger Apr 03 '21 at 14:06
  • Sounds like you want the [`nuxtServerInit` action](https://nuxtjs.org/docs/2.x/directory-structure/store#the-nuxtserverinit-action). – tony19 Apr 04 '21 at 05:11
  • @tony19: Thanks for the comment, but no, I am looking for a solution to run some code _once on server startup_, not _once per request_. – Markus Weninger Apr 04 '21 at 08:42
  • `nuxtServerInit` only runs once on the server, and doesn't run per request in my test. How is that happening for you? – tony19 Apr 05 '21 at 00:28
  • 2
    @tony19 I appreciate your help. Yet, I want to be able to run code exactly once on `nuxt start`, i.e., a single time when I start the server. The `nuxtServerInit` method is executed every time when a vuex-store is set up, i.e., every time a user requests the page. This is not my use case. – Markus Weninger Apr 06 '21 at 13:41

1 Answers1

0

I don't know if this topic is still relevant, but you can solve this using a 'ready' hook:

First create a file like this (./hooks/hooks.js):

export default (nuxtConfig) => ({
    ready: () => {
        // Execute your code here
    }
});

Then add it to your nuxt.config.js:

import hooks from './hooks/hooks';

export default {

    // Other stuff

    hooks: hooks(this),

    // Other stuff

}
Mojo
  • 11
  • 4