react-static: [Bug] Absolute paths leaking in the bundle
Reporting a bug?
Absolute paths are found in the exported bundles. This is surprising, can leak information about your build system, and will cause webpack longterm caching to not work if you build your site in different directories over time (or on multiple servers).
I believe the caching is two fold, part of it would be to use contenthash
and second would be to use relative path for referencing modules.
I know there’s been a lot of work put into using absolute paths over relative paths lately, and I wonder if we could achieve the same result with relative paths instead (unclear what the issue was) ?
Environment
envinfo result
❯ npx envinfo --system --npmPackages 'react*' --binaries --npmGlobalPackages 'react*' --browsers
npx: installed 1 in 1.434s
System:
OS: macOS 10.14.5
CPU: (4) x64 Intel(R) Core(TM) i7-7567U CPU @ 3.50GHz
Memory: 6.09 GB / 16.00 GB
Shell: 5.3 - /bin/zsh
Binaries:
Node: 8.14.0 - ~/n/bin/node
Yarn: 1.16.0 - /usr/local/bin/yarn
npm: 6.9.0 - /usr/local/bin/npm
Browsers:
Chrome: 76.0.3809.132
Firefox: 69.0
Safari: 12.1.1
npmPackages:
react: ^16.9.0 => 16.9.0
react-dom: ^16.9.0 => 16.9.0
react-static: ^7.2.0 => 7.2.2
react-static-plugin-reach-router: ^7.2.0 => 7.2.2
react-static-plugin-sitemap: ^7.2.0 => 7.2.2
react-static-plugin-source-filesystem: ^7.2.0 => 7.2.2
Steps to Reproduce the problem
- Install a “basic” react-static site
❯ react-static create
? What should we name this project? my-static-site
? Select a template below... basic
Creating new react-static project...
cd my-static-site
yarn build
- Look in the
dist/main.*.js
file and find absolute paths. If you’re on Mac, this mean you’d find/Users/[your-user]/...
paths in there.
Another way to see these absolute path is to run yarn serve
and look at the sources
tab in the devtools. You should see absolute path there:
Expected Behavior
I would expect not to find absolute paths in the bundles.
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 3
- Comments: 20 (5 by maintainers)
I’ve played around with node to remove the root paths from the
js
,json
andhtml
indist/
directory and it finally works.Beware hardcoded paths in my snippet – it works for my setup (Unix, MacOS and default /dist & /src paths). UPDATED: there has been some bugs on Linux builds, so I added several fixes to handle empty dirs after file moving operations. So, here is my plugin which runs after bundling and exporting during
build
:Thanks, but nothing is perfect, you know 😉 Check my code update, hope that will help better)
@robertveloso the fix has not yet been released.
You’re awesome.
Why don’t I check out your branch next week and see if I can fix that.
On 2019-09-25 19:59, Nicolas wrote:
My understanding is that react-static uses absolute path to support pointing the
tmpt
,artifacts
anddist
directory to a location that is outside of the repository (for example, support building on Lambda, outputting to the/tmp
directory)). Seems like Gatsby does not support changing the output directory. They’ve tried adding the option but ended up reverting it as it introduced some issues. Unclear to me exactly what the issue(s) were.I’m going to test out using module paths (along with updating the “resolve.modules” Webpack setting) for import paths within generated bundles, while keeping absolute paths anywhere within react-static.
Once that is confirmed to work (if it does), I can revisit https://github.com/react-static/react-static/pull/1232 to see if it actually helps or not.
Hopefully this makes sense!
I ran into this issue yesterday. As @SleeplessByte said, a solution for me may not work for someone else (process.cwd() may not, in fact, be the correct directory, hence the absolute paths). For my build system, though, it is, and the directory was what was varying between builds. Until a solution can be found, adding the following workaround to my node.api.js file fixes this enough that I only track the files that actually changed: