graysonhicks / gatsby-plugin-loadable-components-ssr

Gatsby plugin for using @loadable/component with Gatsby's SSR
49 stars 26 forks source link

Cannot find module '/.cache/page-ssr/loadable-stats-build-javascript.json' #48

Closed Pearce-Ropion closed 2 years ago

Pearce-Ropion commented 2 years ago

Hi,

I am experiencing an error on gatsby develop and gatsby build. Essentially the new location for loadable-stats-build-javascript.json in the latest version cannot be resolved during the SSR phase.

There was an error compiling the html.js component for the development server.
See our docs page on debugging HTML builds for help https://gatsby.dev/debug-html  Cannot find module
'/.cache/page-ssr/loadable-stats-build-javascript.json'

  853 |         }]
  854 |       }]
> 855 |     });
      |      ^
  856 |   } else {
  857 |     children.push(main.icon);
  858 |   }

From my testing, process.cwd() resolves to / during the gatsby SSR phase. Therefore, the resulting json file path in gatsby-ssr is /.cache/page-ssr/loadable-stats-build-javascript.json (note the leading slash / and not ./)

One option to fix this is to export only the relative path from constant.js

export const LOADABLE_STATS_FILE_PATH = path.join(
  // process.cwd() >>  remove this
  GATSBY_CACHE_PATH,
  GATSBY_RENDER_VALIDATION_PREFIX_PATH,
  LOADABLE_STATS_FILE_NAME
)

and then in gatsby-node

filename: path.join(process.cwd(), LOADABLE_STATS_FILE_PATH)

and in gatsby-ssr:

statsFile: path.resolve(LOADABLE_STATS_FILE_PATH)

I am happy to submit a PR if needed.

graysonhicks commented 2 years ago

Thanks for the issue and PR @Pearce-Ropion ! For posterity and clarity, what are the steps to reproduce this? Does it happen on every build or randomly? Is this OS dependent? Thanks and I'll get this merged and released asap!

Pearce-Ropion commented 2 years ago

I'm currently running on Debian 9, so it might be OS specific. I know Linux sometimes does weird stuff with paths.

As for when this happens, it appears to happen on every build and development build on the latest version of gatsby. I had to look through a bunch of the files in the gatsby codebase while researching what was the problem and found a lot of places where they reference the /page-ssr directory like you do during gatsby-node API process. But I couldn't find anywhere where they try to access a file in /page-ssr during the gatsby-ssr API process. So maybe this is just how gatsby works during the SSR step.

I was just testing this in my local repository, but if you'd like I could try to make a repro.

graysonhicks commented 2 years ago

Yea, if you don't mind, that'd be great! I just want to be careful on this pathing because there have been several fixes related to OS here and don't want a regression. Thanks!

yukulelix commented 2 years ago

This is happening to me on Gatsby Cloud too :)

graysonhicks commented 2 years ago

Released 4.1.1 with a fix for this. Thanks @Pearce-Ropion !

reilnuud commented 2 years ago

@graysonhicks – it's back, reverting from 4.1.2 to to 4.1.1 fixes it--leading / strikes again:

abe2da04408c505cbacf11beeca24fbd98a809ba <-- I think this commit broke it

graysonhicks commented 2 years ago

@pieh Any ideas why one breaks the other?

reilnuud commented 2 years ago

My guess would be that the path needs to be resolved still--it's looking for .cache in the system root, not the project root.

Pearce-Ropion commented 2 years ago

Yep, importing from any path that starts with / will import from the system root.

graysonhicks commented 2 years ago

As long as you aren't using DSG, you can stay on 4.1.1

Pearce-Ropion commented 2 years ago

I think you forgot the leading ./ in your import path to make it relative.

graysonhicks commented 2 years ago

Can't tell if that / is important for the DSG aspect: https://github.com/graysonhicks/gatsby-plugin-loadable-components-ssr/pull/50#discussion_r770928104

bwlng commented 2 years ago

@graysonhicks I can test on our project if a beta release is tagged

Leksat commented 2 years ago

Adding . before / does not help.

Module not found: Error: Can't resolve './.cache/page-ssr/loadable-stats-build-javascript.json' in
'/path-to-project/node_modules/gatsby-plugin-loadable-components-ssr'

But notice the path. It tries to find the file in the plugin dir, not in the project root.

Leksat commented 2 years ago

Could we at least reopen this issue?

graysonhicks commented 2 years ago

@pieh Any ideas on this commit causing this problem? Ideally this should work with v4 + DSG.

pieh commented 2 years ago

Path starting with / is intentional as that should resolve to project root in Gatsby. And in test sites that I had as well as at least 1 user case it does work correctly.

To fix the problem I do need to be able to reproduce it - do any of you folks seeing this issue have (public) site I could pull and reproduce this on?

samirergaibi commented 2 years ago

We've got the same issue when updating to 4.1.2. I don't know if this helps but we've got a mono repo setup and from what I can tell it can't find the loadable-stats-build-javascript.json file since it's trying to find it in the plugin directory inside the node_modules folder instead of the projects folder where the .cache directory is as @Leksat pointed out.

Bogdan1001 commented 2 years ago

I got the same issue when I've deleted the .cache folder, I didn't update anything, I'm on Mac 13 m1.

Can't run the project. Any fast workarounds?

reilnuud commented 2 years ago

I got the same issue when I've deleted the .cache folder, I didn't update anything, I'm on Mac 13 m1.

Can't run the project. Any fast workarounds?

If you install 4.1.1, you should be able to avoid this error.

Bogdan1001 commented 2 years ago

@reilnuud that didn't help I've tried. I actually didn't change the version just after the cache was deleted by me it stopped to work not sure how it happened.

@graysonhicks I did some debugging. And I am sure that the 'loadable-stats-build-javascript.json' is created not at the right time(after it was requested by the module).

What steps I've made?

  1. I tried to change the path at the module and that didn't help at all. After that, I did understand that the file does not exist when it was requested.
  2. I've copied 'loadable-stats-build-javascript.json' in another folder and then run 'gatsby develop' and just paste the 'loadable-stats-build-javascript.json' into .cache/page-ssr and after that, all start working well even after the restart, and then once I cleared the cache all broke again. So I've repeated this manual injection and the project work again, and btw the issue does not appear with 'gatsby build'.

Please can you check the lifecycle and ensure that the 'loadable-stats-build-javascript.json' was created in the time as requested by the module? Let me know if I can help you debug it in my environment.

UPD: Can confirm that I could run ^4.1.2 by following the steps above.

graysonhicks commented 2 years ago

One thing that I think needs to happen is the plugin will essentially be ignored in develop because it's not necessary and is where I think this error occurs.

After that, build with v4 and DSG should work and this error would go away in develop.

pieh commented 2 years ago

I opened PR with essentially what @graysonhicks mentioned ("ignoring" develop) - https://github.com/graysonhicks/gatsby-plugin-loadable-components-ssr/pull/51 (so plugin should be no-op for develop, but continue to add Githubissues.

  • Githubissues is a development platform for aggregating issues.