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;








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.










share|improve this question




























    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.










    share|improve this question
























      0












      0








      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.










      share|improve this question














      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 26 at 2:13









      acjayacjay

      19.3k4 gold badges45 silver badges86 bronze badges




      19.3k4 gold badges45 silver badges86 bronze badges






















          1 Answer
          1






          active

          oldest

          votes


















          0














          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.






          share|improve this answer






















            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
            );



            );













            draft saved

            draft discarded


















            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









            0














            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.






            share|improve this answer



























              0














              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.






              share|improve this answer

























                0












                0








                0







                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.






                share|improve this answer













                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.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Apr 3 at 22:00









                acjayacjay

                19.3k4 gold badges45 silver badges86 bronze badges




                19.3k4 gold badges45 silver badges86 bronze badges


















                    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.



















                    draft saved

                    draft discarded
















































                    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.




                    draft saved


                    draft discarded














                    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





















































                    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







                    Popular posts from this blog

                    Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                    SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                    은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현