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.

getInitialProps() missing from dynamic import

See original GitHub issue

Bug report

Describe the bug

When using next/dynamic for dynamic imports of a page-component that contains getInitialProps, the getInitialProps function from the imported dynamic component is missing.

To Reproduce

// src/modules/about.tsx

import React from "react";
import { NextPage, NextPageContext } from "next";

interface Props {
  userAgent?: string;
  test: string;
}

const About: NextPage<Props> = ({ userAgent, test }: NextPageContext) => {
  return (
    <p>User Agent: {userAgent}</p>
    <p>Test: {test}</p>
  );
};

About.getInitialProps = async ({ req }: NextPageContext) => {
  const userAgent = req ? req.headers["user-agent"] : navigator.userAgent;
  console.log('getInitialProps has run')
  return { userAgent, test: "can you see me?" };
};

export default About;
// src/pages/about.ts

import dynamic from "next/dynamic";

const Page = dynamic(() => import(`../modules/about`));

export default Page;

The rendered page is missing the userAgent and test props, and there is no log of getInitialProps having run in the console.

Expected behavior

The dynamically imported Page component contains Page.getInitialProps from About.getInitialProps;

System information

  • OS: macOS
  • Browser: Chrome 79.0.3945.88
  • Version of Next.js: 9.1.6

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
timneutkenscommented, Dec 28, 2019

dynamic() does not return the component itself, it returns a component that has an “eventual” render of the component, and there is no way to copy over static properties reliably (and would cause inconsistency if we tried).

0reactions
balazsorban44commented, Jan 30, 2022

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

getInitialProps() missing from dynamic import #9857 - GitHub
Describe the bug. When using next/dynamic for dynamic imports of a page-component that contains getInitialProps , the getInitialProps function ...
Read more >
getInitialProps - Data Fetching - Next.js
getInitialProps enables server-side rendering in a page and allows you to do initial data population ... import React from 'react' class Page extends...
Read more >
Import a library only on the server side to use in getInitialProps
I would try using fetch in your getInitialProps and make a proxy-call to an api route instead. Inside of your api route, it...
Read more >
The Next.js Handbook - The Valley of Code
This approach lets us use getInitialProps() from within our page component, with the only downside of having to write the component JSX inside...
Read more >
Manual Setup for Next.js - Sentry Documentation
Learn how to set up the SDK manually.
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