-
Notifications
You must be signed in to change notification settings - Fork 33
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
bug: ssrServer ENOENT firestore.proto #152
Comments
I assume I don't need to manually manage dependencies in {
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"lint": "eslint .",
"serve": "firebase emulators:start --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"engines": {
"node": "14"
},
"main": "index.js",
"dependencies": {
"cors": "^2.8.5",
"express": "^4.17.1",
"firebase-admin": "^9.8.0",
"firebase-functions": "^3.15.5",
"stripe": "^8.164.0"
},
"devDependencies": {
"eslint": "^7.6.0",
"eslint-config-google": "^0.14.0",
"firebase-functions-test": "^0.2.0"
},
"private": true
} |
This page is not working either, with the same ENOENT error import { firestore } from "$lib/firebase/client";
import { collection, query, where, getDocs } from 'firebase/firestore';
export async function load({ page, fetch, session, stuff }) {
const profilesRef = collection(firestore(), "profiles");
const profileQuery = query(profilesRef, where("slug", "==", page.params.slug));
const querySnap = await getDocs(profileQuery);
if (querySnap.empty) return {
status: 404,
error: new Error(`Profile not found`),
};
return {
props: {
profile: {
uid: querySnap.docs[0].id,
...querySnap.docs[0].data(),
},
},
};
} But if I navigate to this page using the SvelteKit router (e.g. clicking a link in the client) it works, so definitely entirely related to a bug with the Also worth noting the SSR-aspect of all these pages work fine locally, so nothing to do (as far as I can tell) with including client-side only dependencies or something like that. |
I would suggest starting with updates to both SvelteKit and the adapter. There have been quite a few versions released since those versions you list in the issue. Also, there will be some breaking changes from Kit for the adapter API relatively soon which will affect the compilation of the SSR code, so I expect behaviour to change again in another breaking change. |
Correct, although the SSR code runs on the Cloud Functions, all dependencies for the SSR code are compiled in-line during build phase so you shouldn't need to touch your Cloud Function deps for the Kit APIs or SSR code. |
Many people have had issues with including Firestore directly in |
I updated to
/EDIT I get this error on every page, and the client gets served with Ideas? |
How are you running your app? Just |
Runs fine locally though, just get those errors on the server in the |
can you share more of your actual code? Hard to debug without it. |
Which part of the code should I share? No page can be served by the E.g. a markdown file parsed with mdsvex into svelte that has literally no JS in it triggers this error:
|
firebase.json {
"functions": {
"source": "functions"
},
"hosting": {
"public": "public",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "**",
"function": "ssrServer"
}
],
"predeploy": ["npm run build"]
}
} svelte.config.js import { mdsvex } from "mdsvex";
import mdsvexConfig from "./mdsvex.config.js";
import firebase from "svelte-adapter-firebase";
import preprocess from "svelte-preprocess";
/** @type {import('@sveltejs/kit').Config} */
const config = {
"extensions": [".svelte", ...mdsvexConfig.extensions],
kit: {
adapter: firebase(),
// hydrate the <div id="svelte"> element in src/app.html
target: '#svelte',
vite: {
ssr: {
external: ['firebase']
}
},
preprocess: [
mdsvex(mdsvexConfig),
preprocess({
"postcss": true
})
],
onwarn: (warning, handler) => {
const { code, frame } = warning;
if (code === "a11y-missing-content") return;
console.log(code);
handler(warning);
}
};
export default config;
// Workaround until SvelteKit uses Vite 2.3.8 (and it's confirmed to fix the Tailwind JIT problem)
const mode = process.env.NODE_ENV;
const dev = mode === "development";
process.env.TAILWIND_MODE = dev ? "watch" : "build"; Top of package.json "scripts": {
"dev": "svelte-kit dev",
"build": "npx rimraf public && svelte-kit build --verbose",
"preview": "svelte-kit preview",
"lint": "prettier --check --plugin-search-dir=. . && eslint --ignore-path .gitignore .",
"format": "prettier --write --plugin-search-dir=. ."
}, Firebase versions: I also get this in my console when running
|
This is line 133860 of var require2 = import_module.default.createRequire(import_meta.url); This is further up in the file, and it looks like // node_modules/@firebase/firestore/dist/index.node.mjs
[...]
var import_meta = {}; /EDIT dug out the code for this part of the // This is a hack fix for Node ES modules to use `require`.
// @ts-ignore To avoid using `--module es2020` flag.
const require = module.createRequire(import.meta.url); Could be something there? Some sort of incompatibility with the way |
You can ignore this. I would try bumping your Cloud Function Node.js version from If that doesn't work I have this to say: using If the error is coming from So in your This matters because the packages are different with
Using the Client lib on the Server raises questions around whether the query bypasses the sec rules on the Firestore server or not, and also, if it does go through the security rules, how can the SSR environment know which user initiated the request. Given these factors, I recommend NOT using the Client (web) firestore libs on the Server and switching the use of the It is possible to have an authenticated user pass their auth token to the initial page call that executes the page SSR, so the web lib can be used in SSR envs and know which users initiated the request and perform security rules appropriately, however this wiring needs to be done manually within the Kit request lifecycle and requires handling Firebase auth tokens yourself, which defeats the point of the Firebase. Server data fetch on SSR Client data fetch on client Hope this helps you debug. |
@jthegedus thanks for your advice. What you say about Firestore makes a lot of sense, and keen to do that (though it will obviously take quite some time), but the error I'm now receiving didn't happen before upgrading to Sveltekit Especially given my I tried bumping to Node v16 but the only thing that changes is now I don't get given a reason for the crash in the logs: |
Logically, sure, actually, maybe. Updating Kit actually updates Vite, the Svelte-Vite Plugin, which all effect how code is bundled. The adapters currently then bundle the Vite SSR bundle again using esbuild. So there can be issues between those. You haven't shared your Kit I watch the Kit changelog & PRs for breakages to the adapter API and update accordingly. I currently only test to ensure the Kit todo template works as expected once parsed through the adapter. There is a specific compatibility table of Kit & adapter versions in the readme, so sticking to those might help. I do not currently test how I have been unable to get Firestore working in the way you describe, ever, in any version of Kit & this adapter. With the As I mentioned in my first response
So the aspect of Kit & this adapter that effect SSR code generation is going to change again, so we could spend all week debugging this to only have our efforts rendered useless in a weeks time. Further suggestions:
|
How to do this? Manually edit |
Removing that
Looks like this issue: firebase/firebase-js-sdk#4846 so I used the workaround and dynamically load the functions lib client-side only. |
const functions = require('firebase-functions');
let sveltekitServer;
exports.sveltekit = functions.https.onRequest(async (request, response) => {
if (!sveltekitServer) {
functions.logger.info('Initialising SvelteKit SSR Handler');
sveltekitServer = require('./sveltekit/index').default;
functions.logger.info('SvelteKit SSR Handler initialised!');
}
functions.logger.info('Requested resource: ' + request.originalUrl);
- return sveltekitServer(request, response);
+ let result;
+ try {
+ result = await sveltekitserverServer(request, response);
+ } catch(err) {
+ functions.logger.error(err)
+ }
+ return result; This way you can see the errors when the call the the Kit Server fails. |
This is exactly what I mean when I say Firebase still has many bugs when working with Vite/Kit. I have not been pursuing resolving those bugs here for the The dynamic import of the lib client-side is what I do for all |
@jthegedus off topic in a way, but do you have a code example of importing With this import I get this error:
/EDIT looks like this issue? firebase/firebase-admin-node#1488 |
Just checking in to say that moving ALL firebase calls to dynamic imports (behind a browser check) as you suggested has fixed the problem. Working with It's been a journey. Thanks for the help once again @jthegedus |
This is what I was testing with: // src/lib/firebase/admin.js
import admin from "firebase-admin";
const app = admin.apps.length === 0 ? admin.initializeApp() : admin.apps[0];
const firestore = admin.firestore(app);
export { firestore }; // src/routes/blog/[slug].json.js
import { firestore } from '$lib/firebase/admin';
export async function get(request) {
const slug = request.params.slug;
console.log(`SvelteKit Endpoint: ${request.path}`);
const postPageSnapshot = await firestore.doc(`posts/${slug}`).get(); Importantly I was using |
Glad you got it working. |
Describe the Bug
Have previously released/deployed without any problems. Tried to release tonight and when loading a page that relies on a
load
function with aFirebase
call, it fails. Other pages seem to work.This is the
load
function:This is the
fetchProducts
functionThe
firestore
functionThe errors:
Steps to Reproduce
Have previously deployed without issue, however this is my first release where I've used a
Firestore
call inside a page'sload
function. Mainly looking for a place to start my debugging for something like this.Expected Behaviour
Code should work fine?
svelte-adapter-firebase version
0.9.2
sveltejs/kit version
1.0.0.next.160
The text was updated successfully, but these errors were encountered: