Skip to content
This repository has been archived by the owner on Jan 11, 2023. It is now read-only.

When exported, pass an environment variable to app that it is running in exported mode #1230

Closed
ConProgramming opened this issue May 25, 2020 · 4 comments

Comments

@ConProgramming
Copy link

Is your feature request related to a problem? Please describe.
Right now I'm calling a GraphQL server in preload, which is running during dev but of course not after the app is exported. During the export process the data is fetched correctly and stored in the html as expected, but even when running exported the app continues to try to fetch this data.

Describe the solution you'd like
Be able to pass a flag to a Sapper app that the app is running in exported mode (like process.browser does for browser or server)

How important is this feature to you?
I'm working on a package that simplifies some GraphQL usages for the user in Sapper, but this is a major roadblock.

@benmccann
Copy link
Member

I don't know a lot about how export works, but if it's basically the same as SSR then this sounds like it could be a duplicate of the svelte hydration issue sveltejs/svelte#4308. Once the page has already been rendered there's no need to rerun the scripts on the page except for installing event listeners. I wonder if you use the patched version of Svelte provided there if it will work.

@ConProgramming
Copy link
Author

If I comment out sapper.start({ target: document.querySelector('#sapper') });, that fixes it.... but I feel like there's probably a reason I definitely shouldn't do that

@Conduitry
Copy link
Member

This sounds like it's a bit of an XY problem, so I'm hesitant to lean too far into this proposed solution - but if you can currently pass an environment variable to the sapper export command, it will be visible in the bundler config, and then you can do whatever you want with it - doing compile time string replacements or whatever you want. Rather than adding another bit of to Sapper that needs documenting, you can create it yourself.

@ConProgramming
Copy link
Author

Yep that works thanks @Conduitry

For anyone else:

  • I added 'process.exported': process.env.EXPORTED, to each replace of my rollup.config.js
  • And modified my export script in package.json to "export": "EXPORTED=true sapper export --legacy",
  • And just call process.exported in my Svelte code

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

3 participants