Skip to content

Run code once per node server startup #15341

Answered by leerob
bhavin7181 asked this question in Help
Discussion options

You must be logged in to vote

Hey folks, wanted to mention this solution. I know it's not perfect, but it should unblock you from running code before the Next.js server starts without needing to eject out of the default server.

node --require ./server-preload.js ./node_modules/.bin/next start
#    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#    Load our preload before running `next start`

Source and credit: https://jake.tl/notes/2021-04-04-nextjs-preload-hack

Replies: 32 suggested answers 29 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
3 replies
@kostenickj
Comment options

@hbel
Comment options

@Shrivallabh
Comment options

Comment options

You must be logged in to vote
2 replies
@hbel
Comment options

@pgayvallet
Comment options

Comment options

You must be logged in to vote
1 reply
@destroyer22719
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
3 replies
@khromov
Comment options

@hbel
Comment options

@assetcorp
Comment options

Comment options

You must be logged in to vote
1 reply
@hbel
Comment options

Comment options

You must be logged in to vote
2 replies
@alexweininger
Comment options

@alvitoraidhy
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
3 replies
@mahmoud-hadi
Comment options

@erikmellum
Comment options

@anthonyalayo
Comment options

Comment options

You must be logged in to vote
4 replies
@alexghr
Comment options

@icyJoseph
Comment options

@davidwincent
Comment options

@dshook
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
2 replies
@backbone-link
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
2 replies
@larsqa
Comment options

@Angeschossen
Comment options

Comment options

You must be logged in to vote
4 replies
@Angeschossen
Comment options

@huwyane
Comment options

@Angeschossen
Comment options

@huwyane
Comment options

Comment options

You must be logged in to vote
1 reply
@dzcpy
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@dora-gt
Comment options

Answer selected by leerob
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet