Giter Site home page Giter Site logo

kolchurinvv / vue3-ssr-starter Goto Github PK

View Code? Open in Web Editor NEW

This project forked from dongnaebi/vue3-ssr-starter

0.0 0.0 0.0 123 KB

Prefetch and sync state to client with one line of code, out-of-the-box

License: MIT License

JavaScript 87.54% HTML 1.17% Vue 11.13% CSS 0.16%

vue3-ssr-starter's Introduction

vue3-SSR-starter

Prefetch and sync state to client with one line of code, out-of-the-box

Features

  • vue3
  • SSR
  • vue-router
  • we don't need vuex anymore
  • one line of code to sync state
  • vite2
  • tailwind.css

Project setup and usage

npm i
npm run dev
// localhost:3001

Server prefetch and sync to client with one line of code

It provide syncState object to sync the prefetch data to client, you only need one line of code:

async function setup () {
  // ...

  // request home data in server, and sync to client
  syncState.homeData = syncState.homeData || await request('/api/home')

  // ...
}

Because of the setup hook run in both of server and client, so even server fetch is fail, it will be downgrade to client fetch, complete code:

// see src/pages/index.vue
import { reactive } from 'vue'
import { useSyncState, useRequest } from '../hooks/app'
async function setup () {
  const request = useRequest()
  const syncState = useSyncState()
  // sync data to client, if server fetch is fail, will be fetch in client, see index.html and server.js
  if (!syncState.homeData) {
    syncState.homeData = await request('/api/home')
  }

  const homeData = reactive(syncState.homeData)

  return {
    homeData
  }
}

App hook and business hook

You maybe write a business in many clients, e.g. PC web, mobile web, mini program the business is same, but dom, style and project code is different, such as request, in web we can use axios, but in mini program we can only use wx.request, for business code unification, we need a request function to smoothing platform differences.

so we provide app hooks, you need to write your app hook in every client, but your business hook can be a npm package

app hooks

see in src/hooks/app

interact

interact components is different in every client, so we need to provide a unification API:

import { onMounted } from 'vue'
import { useInteract } from '../hooks/app'
export default {
  setup () {
    const { loading, toast, modal, confirm } = useInteract()
    onMounted(() => {
      loading.open('loading...')
      setTimeout(loading.close, 1000)
      // API: loading.open(text = ''),  loading.close()

      toast('123')
      // API: toast(content = '', type = 'info', duration = 2500)

      modal('this is a modal', 'modal content', 'error')
      // API: modal(title = '', content = '', type = 'info')
  
      confirm({
        title: 'please confirm your action',
        content: 'delete this data?'
      }).then(action => {
        console.log(action)
      })
      /* API: confirm ({
        type = 'info',
        title = '',
        content = '',
        showCancelButton = true,
        cancelButtonText = '取消',
        confirmButtonText = '确定'
      }) */
    })
  }
}

You can reimplement it everywhere, see src/hooks/app/interact.js

session

provide user info or client info, you can also update the session data, and yes, we don't need vuex anymore:

import { onMounted } from 'vue'
import { useSession } from '../hooks/app'
export default {
  setup () {
    const { token, UUID, platform, userProfile, host, isLogin, setToken, setUserProfile } = useSession()
    onMounted(() => {
      console.log(token, UUID, platform, userProfile, host, isLogin)
    })
    function onLogin (token) {
      setToken(token)
    }
    function updateCartCount () {
      setUserProfile({ cartCount: 3 })
    }
  
    return {
      onLogin,
      updateCartCount
    }
  }
}

request

provide a axios instance to fetch data in server and client:

import { useRequest } from '../hooks/app'
async function setup () {
    const request = useRequest()
    
    // API: request(url = '', params = {}, method = 'get', contentType = 'form', headers = {}, responseType = 'json')
    const res = await request('/api/home')
    console.log(res)
  }

business hook

You can refer to src/hooks/business/useCart.js and src/pages/cart.vue, please make it universal

Api proxy and serve port

See config/ssr.config.is

vue3-ssr-starter's People

Contributors

dongnaebi 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.