tauri icon indicating copy to clipboard operation
tauri copied to clipboard

@tauri-apps/[email protected], "__TAURI_METADATA__" in window cause ReferenceError: window is not defined

Open linonetwo opened this issue 2 years ago • 10 comments

https://github.com/tauri-apps/tauri/blob/50576a5e1e83978882d90aca280809da2235a8d5/tooling/api/src/window.ts#L2033

When running headless tests, this might be troublesome.

Should check typeof window !== 'undefined' first.

linonetwo avatar Feb 09 '23 15:02 linonetwo

And there will be Cannot use 'in' operator to search for '__TAURI_METADATA__' in undefined if I manually set global.window = undefined

linonetwo avatar Feb 10 '23 07:02 linonetwo

Looks like this is failing my SvelteKit production builds (open image in new window):

image

This spews out a massive JS chunk for SvelteKit. It mentions tauri at the end:

...b;"__TAURI_METADATA__"in window?b=new s(window.__TAURI_METADATA__.__currentWindow.label,{skip:!0}):(console.warn(`Could not find "window.__TAURI_METADATA__". The "appWindow" value will reference the "main" window label.```

Then it gives a window is not defined:

ReferenceError: window is not defined
    at file:///Users/owen/Projects/yark/yark-pages/node_modules/@tauri-apps/api/chunk-QSWLDHGO.js:1:9678

Owez avatar Mar 02 '23 12:03 Owez

Still having this issue, here's a complete log if it helps: https://pastebin.com/B2qyqrNc

Owez avatar Mar 15 '23 16:03 Owez

The same is also happening here

caiocinel avatar Mar 19 '23 03:03 caiocinel

@Owez @tauri-apps/api does not work with Node.js. With SvelteKit, you should to use adapter-static with the fallback option and set ssr = false

probablykasper avatar Mar 23 '23 20:03 probablykasper

@Owez @tauri-apps/api does not work with Node.js. With SvelteKit, you should to use adapter-static with the fallback option and set ssr = false

I'm using both, the code I wrote had problems with vite's dev ssr on sveltekit erroring so everything I've wrote has been protected inside of an if(browser) as well. For some reason this happens using the static adapter and ssr = false. The issue could be caused by vite trying to run it as node for whatever reason, and then tauri's injected window being ran?

Owez avatar Mar 23 '23 20:03 Owez

ssr = false disables dev SSR. Either way, let's not discuss framework specifics in this issue

probablykasper avatar Mar 23 '23 20:03 probablykasper

Same here in NextJs. Even wrapping components in dynamic doesn't solve the issue, I need to import any @tauri-apps with await import inside the client code completely. Even if that workaround works, it's pretty tedious to use.

gentlementlegen avatar May 19 '23 05:05 gentlementlegen

ssr = false disables dev SSR. Either way, let's not discuss framework specifics in this issue

See #6554 (possible workaround also included)

Owez avatar May 19 '23 08:05 Owez

For those afflicted by this issue when using rspc with Tauri and NextJS (very specific, I know), I've chucked together a workaround transport that avoids importing Tauri at all during build time and server-side. https://gist.github.com/kaylendog/ea3d2ff8607d8433849c6bd431fb39b0

kaylendog avatar May 28 '23 14:05 kaylendog

For folks using sveltekit and/or vite, one workaround is to use vite-plugin-iso-import.

Before

// src/routes/+layout.ts
import { appDataDir } from '@tauri-apps/api/path'
import { appWindow } from '@tauri-apps/api/window'

// ERROR
// [vite] Error when evaluating SSR module /src/routes/+layout.ts: failed to import "@tauri-apps/api/path"
// ReferenceError: navigator is not defined

After

// src/routes/+layout.ts
import { appDataDir } from '@tauri-apps/api/path?client'
import { appWindow } from '@tauri-apps/api/window?client'

if (!import.meta.env.SSR) {
	console.log({ appWindow, appDataDir })
}
// All good, no error

To retain full LSP/intellisense functionality, a couple of steps might be required. One of which is the following:

// src/lib/types/global.d.ts
declare module '@tauri-apps/api/path?client' {
	import all from '@tauri-apps/api/path'
	export = all
}

declare module '@tauri-apps/api/window?client' {
	import all from '@tauri-apps/api/window'
	export = all
}

See the docs for setup info.

This workaround used to be mentioned in the svelte docs, but seems to have missed the cut in a recent docs refactor. Nevertheless, it remains a viable workaround.

thenbe avatar Jan 02 '24 02:01 thenbe

This was fixed in v2 by removing variables like appWindow and solely relying on functions like getCurrent(). Since this is a breaking change we can't backport it to v1.

Until v2 hits stable you'll have to keep using await import() or workarounds like the one posted above this message.

FabianLars avatar Jan 02 '24 14:01 FabianLars

Same issue while using 2.0.0-beta.

bennekrouf avatar Feb 14 '24 11:02 bennekrouf

@bennekrouf window.__TAURI_METADATA__ does not exist in v2 anymore. Also, all window references that could cause a similar issue should be removed now too. Please make sure all your tauri related dependencies are updated to v2.

FabianLars avatar Feb 14 '24 11:02 FabianLars