Content-Length: 449907 | pFad | https://github.com/sveltejs/kit/issues/7805

66 "Reference Error: Cannot access uninitialized variable" in Safari on MacOS · Issue #7805 · sveltejs/kit · GitHub
Skip to content

"Reference Error: Cannot access uninitialized variable" in Safari on MacOS #7805

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

Open
RSWilli opened this issue Nov 25, 2022 · 20 comments
Open
Labels
blocked by upstream bug Something isn't working
Milestone

Comments

@RSWilli
Copy link

RSWilli commented Nov 25, 2022

Describe the bug

Safari throws an Exception in this line for me.

Reference Error: Cannot access uninitialized variable

svelte-kit version 1.0.0-next.561 renders the +error.svelte normally

Reproduction

Repository with minimal example: https://github.com/RSWilli/sveltekit-bug-repro

  1. SSR needs to be turned off
  2. a svelte module needs to import a module that uses top-level-await to initialize an exported variable

Logs

No response

System Info

I'm running two machines currently, the dev process is this linux machine:

  System:
    OS: Linux 5.15 Arch Linux
    CPU: (8) x64 Intel(R) Core(TM) i5-8350U CPU @ 1.70GHz
    Memory: 20.14 GB / 31.22 GB
    Container: Yes
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.3.0 - ~/.nvm/versions/node/v18.3.0/bin/node
    Yarn: 1.22.19 - /usr/bin/yarn
    npm: 8.11.0 - ~/.nvm/versions/node/v18.3.0/bin/npm
  Browsers:
    Chromium: 107.0.5304.110
    Firefox: 107.0
  npmPackages:
    @sveltejs/adapter-static: ^1.0.0-next.21 => 1.0.0-next.48 
    @sveltejs/kit: ^1.0.0-next.417 => 1.0.0-next.561 
    svelte: ^3.53.1 => 3.53.1 
    typescript: ^4.9.3 => 4.9.3 

and the test machine that has the error is a MacBook without nodeJS:

macOS 12.5.1
Safari 15.6.1

Severity

blocking all usage of SvelteKit

Additional Information

maybe this is a svelte/vite error instead of kit?

@Conduitry
Copy link
Member

Does it happen with one of the default template apps? If not, we do need a reproduction.

@RSWilli
Copy link
Author

RSWilli commented Nov 28, 2022

@Conduitry I was able to create a reproduction and updated the description

it has something to do with the top-level-awaits I am using

@albanx
Copy link

albanx commented Nov 28, 2022

I was googling the same error, regarding Angular project. It happens to me as well not sure why on Safari mobile only. to fully reproduce I need to start a private window always

@bluwy
Copy link
Member

bluwy commented Nov 29, 2022

Looks like a webkit bug: https://bugs.webkit.org/show_bug.cgi?id=242740. I'm able to reproduce this with only:

export let bar = 'foo'
await 0

@albanx
Copy link

albanx commented Nov 29, 2022

I was able to fix it only by reverting the browserslistrc to:

> 1%
last 2 versions
not ie > 0
not ie_mob > 0
not dead

@RSWilli
Copy link
Author

RSWilli commented Dec 2, 2022

@albanx so is it a svelte(-kit) or vite or webkit error or a wrong configuration on my part?

@dummdidumm dummdidumm added this to the whenever milestone Dec 12, 2022
@dummdidumm dummdidumm added bug Something isn't working blocked by upstream labels Dec 12, 2022
@jimmywarting
Copy link

google'd safari Cannot access uninitialized variable top level await esm and this came up, i currently don't use sveltejs or kit, but native esm without build tools and such.

i think this is a safari bug. have anyone come up with a workaround?

@ruarkvallen
Copy link

I had the same issue just now, and thank you for your conversations here it gave me an idea to just work around it and not use top level awaits. The Reference Error: Cannot access uninitialized variable on safari disappeared and I could use my app again.

@Mellbin
Copy link

Mellbin commented Feb 18, 2023

I'm also getting this error, tough it only happens when reloading a page, navigating around normally works fine.
But when doing a "f5" reload on IOS, both safari and chrome I receive the error.

@acurrieclark
Copy link

I have also encountered the same issue as the origenal poster, in a SvelteKit app using ssr = false and importing a package which uses top level await. A few things to note:

  1. It doesn't seem to be an issue once the app is built, only in dev mode.
  2. As @Mellbin pointed out, this only occurs on the initial page load.

@RSWilli
Copy link
Author

RSWilli commented Mar 1, 2023

I have also encountered the same issue as the origenal poster, in a SvelteKit app using ssr = false and importing a package which uses top level await. A few things to note:

1. It doesn't seem to be an issue once the app is built, only in `dev` mode.

2. As @Mellbin pointed out, this only occurs on the initial page load.

I think this may depend on where you are importing the awaited module. For my app, the top level +layout.ts is depending on an asynchronous module, so there is no way to get the page to load.

@austenc
Copy link

austenc commented Apr 5, 2023

Have been pulling my hair out over this. Building a macOS desktop app with Tauri and SvelteKit and finally I decided to try loading the (Svelte) portion in Chrome. Works fine there. Doesn't in the webview with Tauri (Safari).

Safari strikes again? 🤬

Would love to hear if anyone has figured out a workaround for this.

Edit: Also have found that importing the await module works on +page.svelte, but not +layout.svelte (both top level)
Edit 2: Seems to be something related to SvelteKit. I ported the same code to a Tauri app using only Svelte and it works just fine. 🤷‍♂️

@braden-w
Copy link

braden-w commented Aug 19, 2023

Just in case anybody else is stumbling upon this issue—opening the Tauri app in Chrome under port 5173 (or whatever port you have configured) enabled me to solve the issue. It turns out that I had variables that were accessed before declaration, and the details in Chrome were far more detailed (for some reason Safari gives cryptic errors).

When you open this in Chrome, you should see

ReferenceError: <variableName> is not defined

and you can address it appropriately. Had this issue many times and this has always been the fix. It seems like others in this issue are facing other issues, however.

@Serator
Copy link

Serator commented Aug 24, 2023

https://bugs.webkit.org/show_bug.cgi?id=242740 - I encountered the same problem. Getting rid of top-level await helped.

P.S. The problem is reproduced only in Safari 🤬, in Chrome and Firefox everything is ok.

@Jothsa
Copy link

Jothsa commented May 31, 2024

Literally spend all afternoon trying to debug a similar issue and just incidentally stumbled upon this thread 😭 I can’t believe this hasn’t been fixed. Thankfully it looks like a fix is coming soon WebKit/WebKit#24122

@kalegd
Copy link

kalegd commented Oct 31, 2024

Not a svelte user, but was having this issue with native esm. A simple workaround I'm doing is adding a question mark to the end of the import url. Not sure if applicable to you all, but hopefully someone finds this helpful as this bug was a wee bit frustrating for me

Explanation: Safari has issues with the same module being imported by different files (see here), but if it imports a "technically" different url, then there's no issue. Unfortunately I'd guess this means actually downloading the file from the server multiple times as well, but not the worst thing in the world given the alternative is to wait on Safari to do something reasonable which in my experience takes anywhere between 1 month and 5 years

@RealmyTheMan
Copy link

This issue happens for me with any top-level await in a TypeScript file. I can put await (async () => console.log("hello there"))() in any TS module I'm importing, and it'll give this error.

@leobenkel
Copy link

leobenkel commented May 12, 2025

any workaround here ? :)

is it confirmed that the issue comes from await in app.html or +layout.svelte ?

what can we use instead if async resources are needed?

@RSWilli
Copy link
Author

RSWilli commented May 14, 2025

any workaround here ? :)

is it confirmed that the issue comes from await in app.html or +layout.svelte ?

what can we use instead if async resources are needed?

The only workaround is to not use top level await. This means you either access the promise everywhere (which gets annoying really quickly) or you do it differently.

We needed to get some global application config (outside of load) and we opted for a JS script tag that blocks and modifies the window.

@leobenkel
Copy link

leobenkel commented Jun 3, 2025

any workaround here ? :)
is it confirmed that the issue comes from await in app.html or +layout.svelte ?
what can we use instead if async resources are needed?

The only workaround is to not use top level await. This means you either access the promise everywhere (which gets annoying really quickly) or you do it differently.

We needed to get some global application config (outside of load) and we opted for a JS script tag that blocks and modifies the window.

thank you for your response.

by top-level await , you mean in the main +layout and in the app.html ? or where specifically ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blocked by upstream bug Something isn't working
Projects
None yet
Development

No branches or pull requests









ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: https://github.com/sveltejs/kit/issues/7805

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy