Skip to content

SvelteKit, Cloudflare & Firebase Errors #3921

Closed
@boojum

Description

@boojum

Describe the bug

When deploying SvelteKit app with basic Firebase initialisation to Cloudflare Pages I'm getting the following errors:

IDBIndex is not defined
ReferenceError: IDBIndex is not defined

After refreshing the browser window errors change to (NOTE: not a real web address):

Failed to fetch dynamically imported module: https://example.com/_app/undefined
TypeError: Failed to fetch dynamically imported module: https://example.com/_app/undefined

At the same time, in console I can see another error:

Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "null". Strict MIME type checking is enforced for module scripts per HTML spec.

It is my understanding that the errors are caused by improper initialisation of Firebase. As I already wrap the initialisation in if (browser) and check Firebase's isSupported I'm not sure if the mistake is mine or is it a bug with sveltekit / cloudflare-adapter.

As locally everything is working just fine, I'm leaning towards an issue with cloudflare-adapter.

Reproduction

  1. Create a Git[hub/lab] project to host the example.
  2. Initialise skeleton project with npm init svelte@next
  3. Install @sveltejs/adapter-cloudflare as a dev dependency.
  4. Change @sveltejs/adapter-auto to @sveltejs/adapter-cloudflare in svelte.config.js.
  5. Install firebase as a regular dependency.
  6. Get your Firebase config from the Firebase Console.
  7. Add to <script> tag of index.svelte:
<script >
	import { getApp, initializeApp } from 'firebase/app';
	import { getAnalytics, isSupported } from 'firebase/analytics';
	import { browser } from '$app/env';

	const firebaseConfig = { /* YOUR FIREBASE CONFIG */ };

	let app;

	if (browser) {
		try {
			app = getApp();
		} catch (error) {
			app = initializeApp(firebaseConfig);
			if (isSupported) getAnalytics(app);
		}
	}
</script>
  1. Commit and push, observe the errors when Cloudflare deploys the app.

System Info

System:
    OS: Linux 5.13 Ubuntu 21.10 21.10 (Impish Indri)
    CPU: (8) x64 11th Gen Intel(R) Core(TM) i7-1195G7 @ 2.90GHz
    Memory: 21.88 GB / 31.07 GB
    Container: Yes
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 17.5.0 - /usr/bin/node
    npm: 8.4.1 - /usr/bin/npm
  Browsers:
    Chrome: 98.0.4758.80
    Firefox: 97.0
  npmPackages:
    @sveltejs/adapter-cloudflare: next => 1.0.0-next.9 
    @sveltejs/kit: next => 1.0.0-next.269 
    svelte: ^3.44.0 => 3.46.4

Severity

blocking all usage of SvelteKit

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions