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.

Can't avoid crash when trying to discriminate between build environment and client side.

See original GitHub issue
    "vue": "3.2.25",
    "vite": "2.8.0",
    "vite-ssg": "0.19.2",

I have a dialog that has a dependency on @a11y/focus-trap which is a web component. As it is a web component, it uses stuff like document.createElement(‘template’). So I need to prevent it from being imported during SSG.

I tried 2 solutions without success :

  1. Test window presence with dynamic import
if (window) {
  import('@a11y/focus-trap')
}

Leads to the following error:

[vite-ssg] An internal error occurred.
[vite-ssg] Please report an issue, if none already exists: https://github.com/antfu/vite-ssg/issues
file:///Users/doe/Projects/front/.vite-ssg-temp/main.mjs:63
if (window) {
^

ReferenceError: window is not defined
    at file:///Users/doe/Projects/front/.vite-ssg-temp/main.mjs:63:1
    at ModuleJob.run (node:internal/modules/esm/module_job:183:25)
    at async Loader.import (node:internal/modules/esm/loader:178:24)
    at async importModuleDynamicallyWrapper (node:internal/vm/module:437:15)
    at async Object.build (/Users/doe/Projects/front/node_modules/vite-ssg/dist/chunks/build.cjs:171:87)
    at async Object.handler (/Users/doe/Projects/front/node_modules/vite-ssg/dist/node/cli.cjs:25:3)
  1. Use ClientOnly

Which is the only way in the documentation to prevent running code in SSG. I used it around all usage of the dialog component that does the import. Sadly it doesn’t change anything.

[vite-ssg] An internal error occurred.
[vite-ssg] Please report an issue, if none already exists: https://github.com/antfu/vite-ssg/issues
file:///Users/doe/Projects/front/.vite-ssg-temp/main.mjs:466
const template = document.createElement("template");
                 ^

ReferenceError: document is not defined
    at file:///Users/doe/Projects/front/.vite-ssg-temp/main.mjs:466:18
    at ModuleJob.run (node:internal/modules/esm/module_job:183:25)
    at async Loader.import (node:internal/modules/esm/loader:178:24)
    at async importModuleDynamicallyWrapper (node:internal/vm/module:437:15)
    at async Object.build (/Users/doe/Projects/front/node_modules/vite-ssg/dist/chunks/build.cjs:171:87)
    at async Object.handler (/Users/doe/Projects/front/node_modules/vite-ssg/dist/node/cli.cjs:25:3)

It would be nice to have an identified solution in the documentation to prevent code from running during SSG.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:13

github_iconTop GitHub Comments

1reaction
userquincommented, May 5, 2022

@earlAchromatic if you want to also use apollo with vite-ssg I have also a patch for it, just check https://github.com/userquin/vitesse-stackter-clean-architect, from #241

0reactions
earlAchromaticcommented, May 5, 2022

@userquin Nice work! Thank you taking a deep dive into it. Patch script is great 😄

Read more comments on GitHub >

github_iconTop Results From Across the Web

Architectural Exclusion: Discrimination and Segregation ...
The built environment is characterized by man-made physical features that make it difficult for certain individuals—often poor people and people ...
Read more >
The Impact of Built Environment on Pedestrian Crashes and ...
The objectives of this study are twofold: 1) to examine the correlation between specific features of the built environment on and around the...
Read more >
Guilty Plea from Webster Man in Empire Boulevard Hit-and ...
ROCHESTER, N.Y. -- A Webster man pleaded guilty Wednesday to a hit-and-run crash on Empire Boulevard in Oct. 2015 where a man riding...
Read more >
Supplemental Document: BIG-IP 11.6.1 Fixes and Known ... - AskF5
Fix: Running the command 'tmsh show sys conn' no longer causes TMM to crash when displaying connections. 560607-3 : Resource Limitation error when...
Read more >
Tourist-spot crash in upstate New York kills 18 in limo and 2 ...
A limousine loaded with revelers headed to a 30th birthday party blew a stop sign at the end of a highway and plowed...
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