question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Static export - initial props on reload

See original GitHub issue

Hi, I have a strange issue

I have created a small repo to reproduce it : https://github.com/mlecoq/nextjs-initialprops-issue

After static export : There is two pages : index & details

In details I want to display query param

import React from 'react';
import Link from 'next/link';

const Details = ({ name }) => <div>Here is my .... name ! {name}</div>;

Details.getInitialProps = async ({ query: { name } }) => {
  return { name };
};

export default Details;

When I click on a link from index (http://localhost) page to details (http://localhost/details/?name=testname), initial props is fired

but when I reload the page, my query param is not displayed in the page, initial props is not called

You ca reproduce the issue here: https://out-fpdblcijhc.now.sh

Your Environment

Tech Version
next 4.1.4
node 8.9.1
OS MacOs High Sierra
browser Chrome

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:7
  • Comments:17 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
timneutkenscommented, Nov 9, 2019

In the latest version of Next.js the querystring will be parsed even if a page did export statically.

3reactions
drKnoxycommented, Nov 27, 2018

Holy Moley, the canary branch is pretty different from master. The fix is pretty much the same so I might submit a PR, but trying to run tests crashed my macbook pro and trying to npm link and test it crashed my app… So the canary flag is about right imo 😉

If anyone in the thread cares to patch this for their install… Basically the router for export isn’t getting the query parameter from the client (window.location), and needs to be.

// /node_modules/next/dist/client/index.js:24
var _querystring = require("querystring");

// /node_modules/next/dist/client/index.js:64
nextExport = _window$__NEXT_DATA__.nextExport,
dynamicIds = _window$__NEXT_DATA__.dynamicIds;

// /node_modules/next/dist/client/index.js:164
case 22:
          exports.router = router = (0, _router2.createRouter)(
            page,
            // this line
            nextExport ? (0, _querystring.parse)(location.search.replace(/^\?/, '')) : query,
            asPath, {
              initialProps: props,
              pageLoader: pageLoader,
              App: App,
              Component: Component,
              ErrorComponent: ErrorComponent,
              err: initialErr
            }
          );
Read more comments on GitHub >

github_iconTop Results From Across the Web

Static export - initial props on reload · Issue #3314 - GitHub
Hmm, it seems getInitialProps does get called in a static export if one enters the page with link, but is NOT called when...
Read more >
getInitialProps - Data Fetching - Next.js
getInitialProps enables server-side rendering in a page and allows you to do initial data population, it means sending the page with the data...
Read more >
Next.js getInitialProps() method not called when reloading ...
See above and no - there is no way to have getInitialProps run on a fully static, exported site. You'd need to switch...
Read more >
Refreshing Server-Side Props - Next.js - Josh W Comeau
React uses that data as the initial props when rendering the new page, in-browser.
Read more >
How to reload a Next.js page's initial props without reloading ...
import { NextPage, NextPageContext } from 'next'; import React, { useState, PropsWithChildren, ComponentType } from 'react'; export interface ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found