Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't Build Next v9 With Non-React Components #8617

Closed
DovahBrownies opened this issue Sep 4, 2019 · 21 comments
Closed

Can't Build Next v9 With Non-React Components #8617

DovahBrownies opened this issue Sep 4, 2019 · 21 comments

Comments

@DovahBrownies
Copy link

DovahBrownies commented Sep 4, 2019

Bug report

Describe the bug

After upgrading from v8.9 to v9, Next will refuse to build my project because of the presence of non-react components. There doesn't seem to be a setting I can add in next's config to get around this, forcing me to completely destructure my pages. Isn't there a way to disabled automatic static optimization?
Spectrum question here.

Error:

> Build error occurred
Error: automatic static optimization failed: found pages without a React Component as default export in

proceeded by the list of all the files that aren't react components (AKA the style and test files).

To Reproduce

I have setup a file structure as such:

/pages
└── /MyPage
    └── index.js // Standard React exported page
    └── styles.js // EmotionJS styling for this specific page
    └── MyPage.test.js // Jest test file

Simply run next build or yarn build

Expected behavior

To build normally, like in Next v8.

@timneutkens
Copy link
Member

This is not a bug and expected behavior. In Next.js 8 it would actually build and expose MyPage.test.js and styles.js as pages, which slow down builds and are accessible publicly.

The main constraint of Next.js is that pages are pages.

@zxiest
Copy link

zxiest commented Sep 4, 2019

Hello @timneutkens ; so there's no workaround to this? ie: Next not exposing these files?

@timneutkens
Copy link
Member

@zxiest this has been discussed many times in different GitHub issues. You shouldn't put components into pages as it:

  • Slows down builds, as every file is a page
  • Exposes the files publicly
  • Affects code splitting in a negative way

Most recent discussion is here #4315 (comment)

@zxiest
Copy link

zxiest commented Sep 4, 2019

Thanks for the link. We shall restructure accordingly.

@zxiest
Copy link

zxiest commented Sep 4, 2019

Rethinking this, would it not be possible to add an ignorePattern (different name of course) by which Next can ignore *.test.js or *.style.js ?

This should not harm build speeds, not expose the files publicly (obviously this needs to be supported), and not harm code splitting.

It's very annoying having to separate the files in many directories and looking for them every time you need to do an edit. I am a big fan of convention over configuration coming from the Rails world but have noticed it's much more efficient to keep component assets in the same directory.

@timneutkens
Copy link
Member

timneutkens commented Sep 4, 2019

This has been discussed many times before as said and that still introduces security issues etc.

You can keep pages only to expose routes though and then keep a separate directory with a structure that you want.

@zxiest
Copy link

zxiest commented Sep 4, 2019

Thank you for taking the time to respond =)

This is what we have ended up doing. We're following this structure outside the /pages directory and using /pages for the aforementioned purpose only.

@istarkov
Copy link
Contributor

istarkov commented Sep 4, 2019

But what to do with relay generated files placed under __generated__ subfolder? Having that we use queries at pages level?

@istarkov
Copy link
Contributor

istarkov commented Sep 4, 2019

And yes relay-compiler allow to use one folder for this files, but we have hundreds of such. Not easy to import, find etc. So using __generated__ subfolder is the main and possibly only option for us. Ive read links above and don't understand how ignored!!! files can somehow affect anything?

@istarkov
Copy link
Contributor

istarkov commented Sep 4, 2019

For now I excluded __generated__ files using webpack config option

    webpack: (config /* : any */) => {
      const originalEntry = config.entry;
      config.entry = async () => {
        const entries = await originalEntry();

        const keys = Object.keys(entries);
        keys.forEach(key => {
          if (key.includes('/__generated__/')) {
            delete entries[key];
          }
        });

        return entries;
      };

      return config;
    },
  })

So can be used as a solution

thibaultboursier added a commit to thibaultboursier/nextjs-with-relay-modern that referenced this issue Dec 1, 2019
thibaultboursier added a commit to thibaultboursier/nextjs-with-relay-modern that referenced this issue Dec 1, 2019
try to run relay on build step

add graphql-cli dependency

make some changes to fix NextJS error (vercel/next.js#8617)

prevent default on link

trigger error on server

trigger error on server

trigger error on server

trigger error on server

trigger error on server

trigger error on server
@webdeb
Copy link

webdeb commented Jan 1, 2020

Just an idea for disappointed folks, that came previously from create-react-app (like me). And would like to have a directory per page:

Put your pages code inside src/pages

<root>/
  src/
    pages/
      welcome/
        index.js,
        Welcome.messages.js
        styles.js

And reference pages/welcome -> src/pages/welcome

// <root>/pages/welcome.js
export { default } from './src/pages/welcome'

IMHO, you also get more control, and its easier to reason about what will be available in prod, since the pages/ directory stays really lean and simple.

@benoror
Copy link

benoror commented May 20, 2020

Any reason this is allowed in development mode?

@hiukky
Copy link

hiukky commented Jun 21, 2020

This does not make sense to be allowed in the development environment since it applies to the production environment.

@hiukky
Copy link

hiukky commented Jun 21, 2020

image

One solution I found at the moment was to leave my views in my src directory and to export only the default export of each component export { default } from '../src/views/About'. as I like to separate the styles, types and tests files in each module a was the best solution at the moment.

@TrySound
Copy link
Contributor

@istarkov's hack stopped working

@timneutkens
Copy link
Member

@TrySound it likely has a slash too many /__generated__/ should be __generated__/.

@TrySound
Copy link
Contributor

@timneutkens This is how the key look like pages/__generated__/AppQuery.graphql. Your suggestion does not solve the problem.

@davidcalhoun
Copy link

Really went down the rabbit hole with this one. Got my page written, tests written and passing, and everything built fine in dev (next dev). Everything appeared to be working, but it was only when it came to build (next export) that I ran into issues.

I first tried to add a custom Webpack config. After adding IgnorePlugin I ran into issues with Webpack 4, so I had to opt into Webpack 5. I finally got Webpack to successfully ignore Jest tests (e.g. /pages/foo/index.spec.tsx)and Storybook stories (e.g. /pages/foo/index.stories.tsx), then finally ran up against this issue (Error: Build optimization failed: found page without a React Component as default export in...).

This has almost caused me to bail out on using Next.js for now. It's obvious that many folks would like to co-locate files next to pages for better developer experience. It's similar to the greatly improved developer experience of co-locating CSS files alongside their React components, instead of in some far-away directory (or back in the old days, one massive mysterious file).

From reading other threads and this one, it seems that Next.js maintainers may be open to adding some sort of ignore or include/exclude to next.config.js, which would be great, and I hope to see that someday.

Other than this issue, everything else in Next.js has worked out pretty nicely, and it's really lovely. Thank you!

@hiukky That solution seems somewhat reasonable, I'll give it a try. Thanks!

@cristian-eriomenco

This comment has been minimized.

wixo added a commit to openpolitica/open-wizard that referenced this issue Jan 15, 2021
NextJS won't let us build unless _every page is a page_.
Meaning that we can't have styles.js files around.
This is a workaround, explained here: vercel/next.js#8617 (comment)
@OzzieOrca
Copy link
Contributor

OzzieOrca commented Mar 3, 2021

https://nextjs.org/docs/api-reference/next.config.js/custom-page-extensions seems like a good way to tackle this for now. It requires you to rename all your pages to MyPage.page.tsx but then Next.js ignores all files not named with the .page extension. Here's an example config:

pageExtensions: ['page.tsx', 'page.ts', 'page.jsx', 'page.js'],

Looks like #8454 (comment) discovered this first.

kodiakhq bot pushed a commit that referenced this issue Aug 27, 2021
…omponents (#22740)

Feel free to edit this/suggest changes as you see fit to match the style of your docs or how you want this feature represented.

There seemed to be many issues regarding colocating non-page files with page components in  the `pages` directory where `pageExtensions` was finally suggested as a solution. I think it would be great to document it better since it seems to be a often requested workflow to colocate test, generated files, styles, and other files needed by page components in the `pages` directory. The note that exists in the docs currently alludes to the ability to do this but it doesn't contain much context that would help make it more discoverable.

Relevant issues:
#11113 (reply in thread)
#8454 (comment)
#8617 (comment)
#3728 (comment)
@balazsorban44
Copy link
Member

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.

@vercel vercel locked as resolved and limited conversation to collaborators Jan 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests