Launch-time initialization in Next.js static/exported siteWhat is a static site generator?How to preview a static site?vscode launch config for next.js appModifying webpack in Next.js for static pageNext.js - Broken listeners after static exportHow can I export static HTML pages from next.js when they need data from a third-party API?Static site generator that does not need node to compileDoes Next.js ship all the React code to the browser on initial load?Exporting multiple static HTML from next.js and reactNext.js dynamic page params for static export
Optimization terminology: "Exact" v. "Approximate"
Why are Hobbits so fond of mushrooms?
Credit score and financing new car
How to tell someone I'd like to become friends without letting them think I'm romantically interested in them?
Why do people keep referring to Leia as Princess Leia, even after the destruction of Alderaan?
Why doesn't sea level show seasonality?
Why presheaves are generalized objects?
Why are they 'nude photos'?
Is a request to book a business flight ticket for a graduate student an unreasonable one?
How is angular momentum conserved for the orbiting body if the centripetal force disappears?
How would vampires avoid contracting diseases?
How to loop for 3 times in bash script when docker push fails?
Why did Harry Potter get a bedroom?
Was I subtly told to resign?
Confirming the Identity of a (Friendly) Reviewer After the Reviews
Contexte et orthographe du mot « feedback »
What's the point of having a RAID 1 configuration over incremental backups to a secondary drive?
Find The One Element In An Array That is Different From The Others
How would my creatures handle groups without a strong concept of numbers?
How can I effectively communicate to recruiters that a phone call is not possible?
RPI3B+: What are the four components below the HDMI connector called?
Would dual wielding daggers be a viable choice for a covert bodyguard?
Machine learning and operations research projects
Print the last, middle and first character of your code
Launch-time initialization in Next.js static/exported site
What is a static site generator?How to preview a static site?vscode launch config for next.js appModifying webpack in Next.js for static pageNext.js - Broken listeners after static exportHow can I export static HTML pages from next.js when they need data from a third-party API?Static site generator that does not need node to compileDoes Next.js ship all the React code to the browser on initial load?Exporting multiple static HTML from next.js and reactNext.js dynamic page params for static export
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I'm trying to use Next to power an Electron app. electron-next uses Next's static site mode for its production build, which calls getInitialProps
at build-time, rather than launch-time.
start.js (initially rendered page)
import Link from 'next/link'
export default function Start(date)
return (
<div>
<div>Date is date</div> /* <- will always be the build time */
<Link href="/about">
<a>Take me to the About page</a>
</Link>
</div>
)
Start.getInitialProps = () =>
return
date: "" + new Date()
Interestingly, using Link
to navigate elsewhere does, in fact, result in a dynamic getInitialProps
call.
about.js
import Link from 'next/link'
export default function About(date)
return (
<div>
<div>Date is date</div> /* <- will be the time the link was clicked */
<div>Important info about this app</div>
</div>
)
About.getInitialProps = () =>
return
date: "" + new Date()
Is there a non-hacky way to get dynamic behavior for the initial route? I imagine this would have plenty of use cases in static sites, too.
next.js static-site
add a comment |
I'm trying to use Next to power an Electron app. electron-next uses Next's static site mode for its production build, which calls getInitialProps
at build-time, rather than launch-time.
start.js (initially rendered page)
import Link from 'next/link'
export default function Start(date)
return (
<div>
<div>Date is date</div> /* <- will always be the build time */
<Link href="/about">
<a>Take me to the About page</a>
</Link>
</div>
)
Start.getInitialProps = () =>
return
date: "" + new Date()
Interestingly, using Link
to navigate elsewhere does, in fact, result in a dynamic getInitialProps
call.
about.js
import Link from 'next/link'
export default function About(date)
return (
<div>
<div>Date is date</div> /* <- will be the time the link was clicked */
<div>Important info about this app</div>
</div>
)
About.getInitialProps = () =>
return
date: "" + new Date()
Is there a non-hacky way to get dynamic behavior for the initial route? I imagine this would have plenty of use cases in static sites, too.
next.js static-site
add a comment |
I'm trying to use Next to power an Electron app. electron-next uses Next's static site mode for its production build, which calls getInitialProps
at build-time, rather than launch-time.
start.js (initially rendered page)
import Link from 'next/link'
export default function Start(date)
return (
<div>
<div>Date is date</div> /* <- will always be the build time */
<Link href="/about">
<a>Take me to the About page</a>
</Link>
</div>
)
Start.getInitialProps = () =>
return
date: "" + new Date()
Interestingly, using Link
to navigate elsewhere does, in fact, result in a dynamic getInitialProps
call.
about.js
import Link from 'next/link'
export default function About(date)
return (
<div>
<div>Date is date</div> /* <- will be the time the link was clicked */
<div>Important info about this app</div>
</div>
)
About.getInitialProps = () =>
return
date: "" + new Date()
Is there a non-hacky way to get dynamic behavior for the initial route? I imagine this would have plenty of use cases in static sites, too.
next.js static-site
I'm trying to use Next to power an Electron app. electron-next uses Next's static site mode for its production build, which calls getInitialProps
at build-time, rather than launch-time.
start.js (initially rendered page)
import Link from 'next/link'
export default function Start(date)
return (
<div>
<div>Date is date</div> /* <- will always be the build time */
<Link href="/about">
<a>Take me to the About page</a>
</Link>
</div>
)
Start.getInitialProps = () =>
return
date: "" + new Date()
Interestingly, using Link
to navigate elsewhere does, in fact, result in a dynamic getInitialProps
call.
about.js
import Link from 'next/link'
export default function About(date)
return (
<div>
<div>Date is date</div> /* <- will be the time the link was clicked */
<div>Important info about this app</div>
</div>
)
About.getInitialProps = () =>
return
date: "" + new Date()
Is there a non-hacky way to get dynamic behavior for the initial route? I imagine this would have plenty of use cases in static sites, too.
next.js static-site
next.js static-site
asked Mar 26 at 2:13
acjayacjay
19.3k4 gold badges45 silver badges86 bronze badges
19.3k4 gold badges45 silver badges86 bronze badges
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I ended up not using getInitialProps
at all. Instead, I'm using a React hook. It works basically like this:
async function useModel()
const modelRef = useRef(null)
// This hook will render at build-time by Next.js's static site, in which
// case the conditional loading of the model will never happen.
//
// At startup-time, it will be re-renderered on the Electron renderer thread,
// at which time, we'll actually want to load data.
if (process.browser && !modelRef.current)
const m = new Model()
await m.init() // <- Assumed to have some async fetching logic
modelRef.current = m
return modelRef.current
Then, the top-level component can easily use the presence of the model to determine what to do next:
function Start()
const model = useModel()
if (!model)
return <div>Loading...</div>
else
return <MyProperUI model=model />
Or, you could easily rig it up to show an unpopulated default UI, or whatever.
So basically, use getInitialProps
for code you want to run exactly once, server-side/build-time or client-side. Otherwise, use other means of initialization. As seen here, hooks allow for this with pretty minimal boilerplate.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55348900%2flaunch-time-initialization-in-next-js-static-exported-site%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I ended up not using getInitialProps
at all. Instead, I'm using a React hook. It works basically like this:
async function useModel()
const modelRef = useRef(null)
// This hook will render at build-time by Next.js's static site, in which
// case the conditional loading of the model will never happen.
//
// At startup-time, it will be re-renderered on the Electron renderer thread,
// at which time, we'll actually want to load data.
if (process.browser && !modelRef.current)
const m = new Model()
await m.init() // <- Assumed to have some async fetching logic
modelRef.current = m
return modelRef.current
Then, the top-level component can easily use the presence of the model to determine what to do next:
function Start()
const model = useModel()
if (!model)
return <div>Loading...</div>
else
return <MyProperUI model=model />
Or, you could easily rig it up to show an unpopulated default UI, or whatever.
So basically, use getInitialProps
for code you want to run exactly once, server-side/build-time or client-side. Otherwise, use other means of initialization. As seen here, hooks allow for this with pretty minimal boilerplate.
add a comment |
I ended up not using getInitialProps
at all. Instead, I'm using a React hook. It works basically like this:
async function useModel()
const modelRef = useRef(null)
// This hook will render at build-time by Next.js's static site, in which
// case the conditional loading of the model will never happen.
//
// At startup-time, it will be re-renderered on the Electron renderer thread,
// at which time, we'll actually want to load data.
if (process.browser && !modelRef.current)
const m = new Model()
await m.init() // <- Assumed to have some async fetching logic
modelRef.current = m
return modelRef.current
Then, the top-level component can easily use the presence of the model to determine what to do next:
function Start()
const model = useModel()
if (!model)
return <div>Loading...</div>
else
return <MyProperUI model=model />
Or, you could easily rig it up to show an unpopulated default UI, or whatever.
So basically, use getInitialProps
for code you want to run exactly once, server-side/build-time or client-side. Otherwise, use other means of initialization. As seen here, hooks allow for this with pretty minimal boilerplate.
add a comment |
I ended up not using getInitialProps
at all. Instead, I'm using a React hook. It works basically like this:
async function useModel()
const modelRef = useRef(null)
// This hook will render at build-time by Next.js's static site, in which
// case the conditional loading of the model will never happen.
//
// At startup-time, it will be re-renderered on the Electron renderer thread,
// at which time, we'll actually want to load data.
if (process.browser && !modelRef.current)
const m = new Model()
await m.init() // <- Assumed to have some async fetching logic
modelRef.current = m
return modelRef.current
Then, the top-level component can easily use the presence of the model to determine what to do next:
function Start()
const model = useModel()
if (!model)
return <div>Loading...</div>
else
return <MyProperUI model=model />
Or, you could easily rig it up to show an unpopulated default UI, or whatever.
So basically, use getInitialProps
for code you want to run exactly once, server-side/build-time or client-side. Otherwise, use other means of initialization. As seen here, hooks allow for this with pretty minimal boilerplate.
I ended up not using getInitialProps
at all. Instead, I'm using a React hook. It works basically like this:
async function useModel()
const modelRef = useRef(null)
// This hook will render at build-time by Next.js's static site, in which
// case the conditional loading of the model will never happen.
//
// At startup-time, it will be re-renderered on the Electron renderer thread,
// at which time, we'll actually want to load data.
if (process.browser && !modelRef.current)
const m = new Model()
await m.init() // <- Assumed to have some async fetching logic
modelRef.current = m
return modelRef.current
Then, the top-level component can easily use the presence of the model to determine what to do next:
function Start()
const model = useModel()
if (!model)
return <div>Loading...</div>
else
return <MyProperUI model=model />
Or, you could easily rig it up to show an unpopulated default UI, or whatever.
So basically, use getInitialProps
for code you want to run exactly once, server-side/build-time or client-side. Otherwise, use other means of initialization. As seen here, hooks allow for this with pretty minimal boilerplate.
answered Apr 3 at 22:00
acjayacjay
19.3k4 gold badges45 silver badges86 bronze badges
19.3k4 gold badges45 silver badges86 bronze badges
add a comment |
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55348900%2flaunch-time-initialization-in-next-js-static-exported-site%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown