这段代码是否在 sveltekit 中为客户端隐藏了?

Is this code hidden for the client in sveltekit?

api 密钥是否对用户隐藏?

# $lib/config.js

import { initializeApp } from 'firebase/app';
import { getFirestore } from "firebase/firestore/lite";
        
        
const firebaseConfig = {
      apiKey: "my-key",
};
        
const app = initializeApp(firebaseConfig);
        
export const db = getFirestore(app);


#index.svelte

import {db} from "$lib/config"

db.get...and so on

试图了解如何处理您想隐藏在 sveltekit 中的东西,因为如果需要通过源代码,用户通常可以看到 js。

您需要将 index.svelte 文件分成两部分。 Public 并提供数据。

顺便说一句:您的代码建议不会起作用,因为代码不会始终在服务器 (SSR) 上执行。您无法在浏览器中访问数据库。

index.json.js,始终在服务器上 运行,您可以在其中验证用户并准备数据。 (阅读更多关于 endpoints


import {db} from "$lib/config"

db.get...and so on

index.svelte,加载准备好的数据

<script context="module">
    /** @type {import('@sveltejs/kit').Load} */
    export async function load({ params, fetch, session, stuff }) {
        const url = `index.json`;
        const res = await fetch(url);

        if (res.ok) {
            return {
                props: {
                    article: await res.json()
                }
            };
        }

        return {
            status: res.status,
            error: new Error(`Could not load ${url}`)
        };
    }
</script>