Skip to content
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

ReferenceError: __WC_CONFIG__ is not defined #290

Closed
AriPerkkio opened this issue Aug 29, 2024 · 6 comments · Fixed by #293
Closed

ReferenceError: __WC_CONFIG__ is not defined #290

AriPerkkio opened this issue Aug 29, 2024 · 6 comments · Fixed by #293
Labels
bug Something isn't working

Comments

@AriPerkkio
Copy link
Member

Describe the bug

Freshly created tutorials without any modifications are running into __WC_CONFIG__ is not defined errors. This is coming up randomly and looks like possible race condition. When page is refreshed with cache clearing the error disappears, but normal refreshes run into this error immediatelly.

This only affects dev mode.

Link to a StackBlitz project which shows the error

No response

Steps to reproduce

  1. pnpm create tutorial
  2. Start in dev mode
  3. Open page in browser
  4. Keep refreshing page and check JS console

Expected behavior

Error should not happen.

Screenshots

image

Platform

  • TutorialKit version: 0.2.0

Additional context

No response

@AriPerkkio AriPerkkio added the bug Something isn't working label Aug 29, 2024
@Nemikolh
Copy link
Member

Could this be a bug with Astro? I wonder if we also see this issue with an older version of Astro.

I'm wondering because this is literally the first thing we set in the vite config and I don't think this has ever really changed:

async 'astro:config:setup'(options) {
const { injectRoute, updateConfig, config } = options;
updateConfig({
server: {
headers: {
'Cross-Origin-Embedder-Policy': isolation ?? 'require-corp',
'Cross-Origin-Opener-Policy': 'same-origin',
},
},
vite: {
optimizeDeps: {
entries: ['!**/src/(content|templates)/**'],
include: process.env.TUTORIALKIT_DEV ? [] : ['@tutorialkit/react'],
},
define: {
__ENTERPRISE__: `${!!enterprise}`,
__WC_CONFIG__: enterprise ? JSON.stringify(enterprise) : 'undefined',

(also if that helps bisecting the combo that breaks, I've never encountered that issue when I was doing demos back in June)

@AriPerkkio
Copy link
Member Author

This issue isn't present on 0.1.2 but appears first in 0.1.3.

0.1.2...0.1.3

@AriPerkkio
Copy link
Member Author

AriPerkkio commented Aug 29, 2024

This is likely introduced in #219.

When import of './OpenInStackblitzLink.astro' is removed here, the issue disappears:

import OpenInStackblitzLink from './OpenInStackblitzLink.astro';
import Logo from './Logo.astro';
import { useAuth } from './setup';

There's two depedency chains for the setup.ts that tries to access __WC_CONFIG__:

TopBarWrapper.astro > setup
TopBarWrapper.astro > OpenInStackblitzLink.astro > webcontainer > setup

The first one works fine. But the second on comes from <script> tag. Maybe Astro doesn't apply Vite's define transforms there? I'll keep looking... 👀

@AriPerkkio
Copy link
Member Author

AriPerkkio commented Aug 29, 2024

Maybe Astro doesn't apply Vite's define transforms there? I'll keep looking...

Something weird going on there. Tested this in minimal Astro reproduction:

import { defineConfig } from "astro/config";

export default defineConfig({
  vite: {
    define: {
      __MY_DEFINE__: '"Value set in config"',
    },
  },
});
---
const frontmatter = __MY_DEFINE__;
---

<h1>My Component</h1>

<script define:vars={{frontmatter}}>
  console.log({frontmatter, scriptScope: globalThis.__MY_DEFINE__})

  setTimeout(() => {
    console.log({frontmatter, scriptScope: globalThis.__MY_DEFINE__})
  }, 1000)
</script>

And we get:
image

Looks like Vite's dev-time define is loaded after the Astro component is. Looks like bug to me.

I'll try to find quick solution for us here so that we can release fix as soon as possible. Maybe convert the component into React one. 🤔

@AriPerkkio
Copy link
Member Author

withastro/astro#11874

@Nemikolh
Copy link
Member

Oh good find! Thanks for digging in 🤩

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