GithubHelp home page GithubHelp logo

nuxt3-boilerplate's Introduction

vite-ssr-boilerplate

vite vue nodejs ssr boilerplate

.env文件说明参考

为了防止意外地将一些环境变量泄漏到客户端,只有以 VITE_ 为前缀的变量才会暴露给经过 vite 处理的代码。例如下面这个文件中:

DB_PASSWORD=foobar
VITE_SOME_KEY=123

只有 VITE_SOME_KEY 会被暴露为 import.meta.env.VITE_SOME_KEY 提供给客户端源码,而 DB_PASSWORD 则不会。

由于任何暴露给 Vite 源码的变量最终都将出现在客户端包中,VITE_* 变量应该不包含任何敏感信息。

express 路由

server/controller目录下的文件会自动注册为express路由,路由前缀为/api

# server/controller
- user.ts                      // 最终生成的路由为 /api/user
- common/index.ts             // 最终生成的路由为 /api/common
- config/env/index.ts         // 最终生成的路由为 /api/config/env

client only组件

注: 目前没有找到官方提供的方案 ,如后续更新,以官方为主

import.meta.env.SSR仅用于场景判断,最后的组件还是会打包到entry-server.js,同样会在服务端执行;若想只在浏览器端执行该组建,目前可以参考以下方案;

# src/utils.ts
export function ClientOnlyComponent(lazyModule: () => Promise<any>) {
    return import.meta.env.SSR ? () => null : defineAsyncComponent(lazyModule)
}

# page.vue

import { ClientOnlyComponent } from '@/utils'
export default defineComponent({
    components: {
        DashboardEchartsVue: ClientOnlyComponent(
            () => import('./components/MyEcharts.vue'),
        ),
    },
})

useAsyncData 同时支持服务端和客户端请求

如果ssr场景,则csr端不会发起ajax请求,通过window.__KALI_DATA__.data保存各页面的数据;如果纯csr(vue-router跳转页面),没有ssr的干预,则由客户端发起ajax请求

<script lang="ts" setup>
    import useAsyncData from '@/utils/useAsyncData'
    const newsDetail = ref<any>({})
    const asyncData = await useAsyncData('aboutQuery', () =>
        queryNewsDetail(id as string),
    )
    if (asyncData) {
        newsDetail.value = asyncData
    }
</script>

nuxt3-boilerplate's People

Contributors

dayao2525 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.