Skip to content
This repository has been archived by the owner on Jul 10, 2024. It is now read-only.

Commit

Permalink
feat: add install page
Browse files Browse the repository at this point in the history
  • Loading branch information
Gem Xli committed Mar 7, 2021
1 parent 0d4bc61 commit 257d137
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 19 deletions.
11 changes: 9 additions & 2 deletions client/entry-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,15 @@ interface RenderedContext {
modules: string[]
}

export async function render(url: string, manifest: SSRManifest) {
const initialState = {}
export async function render(
url: string,
manifest: SSRManifest,
ssrContext: any
) {
const initialState = {
isInstalled: ssrContext.isInstalled
}

const { app, router } = await createApp(initialState)

// set the router to the desired URL before rendering
Expand Down
12 changes: 0 additions & 12 deletions client/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,3 @@ export async function createApp(initialState: Record<string, any>) {

return { app, router }
}

// export default viteSSR(
// App,
// { routes },
// async ({ app, initialState, router }) => {

// r.beforeEach((to) => {
// to.meta.app = app
// to.meta.store = store
// })
// }
// )
22 changes: 20 additions & 2 deletions client/pages/error.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,30 @@
</template>

<script>
import { defineComponent } from 'vue'
import { defineComponent, onMounted, ref } from 'vue'
import { useRouter } from 'vue-router'
import { isSSR } from '~/utils'
export default defineComponent({
setup() {
const errorMsg = ref('')
if (!isSSR) {
const isInstalled = window.__INITIAL_STATE__?.isInstalled
const router = useRouter()
if (!isInstalled) {
router.push('/install')
}
}
onMounted(() => {
errorMsg.value =
window.__INITIAL_STATE__?.storeState?.error || 'Unknown Error'
})
return {
errorMsg: window.__INITIAL_STATE__?.storeState?.error || 'Unknown Error'
errorMsg
}
}
})
Expand Down
6 changes: 6 additions & 0 deletions client/pages/install.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<template>
<div class="install">
<span></span>
install page
</div>
</template>
18 changes: 17 additions & 1 deletion server/halo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import LRU from 'lru-cache'
import proxy from 'koa-better-http-proxy'
import { Context, Middleware } from 'koa'
import { createMarkdownRenderer } from './markdown'
import axios from 'axios'
import axios, { AxiosInstance } from 'axios'

const mdCache = new LRU({
// 24 h
Expand Down Expand Up @@ -84,13 +84,16 @@ export function haloProxy(proxyConf: any): Middleware {
}
}

let axiosInstance: AxiosInstance | null = null

function createHaloProxy(opt: {
target: string
accessKey: string
}): Middleware {
const instance = axios.create({
baseURL: opt.target
})
axiosInstance = instance

instance.interceptors.request.use((conf) => {
if (conf.url?.match('/api/admin')) {
Expand All @@ -114,3 +117,16 @@ function createHaloProxy(opt: {
ctx.body = res.data
}
}

let cacheInstalled = false
export async function checkInstall() {
if (cacheInstalled) {
return true
}

const res = await axiosInstance?.get('/api/admin/is_installed')

cacheInstalled = !!res?.data?.data

return cacheInstalled
}
23 changes: 21 additions & 2 deletions server/vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ import fs from 'fs'
import serve from 'koa-static'
import { Middleware } from 'koa'
import { ViteDevServer } from 'vite'
import { checkInstall } from './halo'

const debug = require('debug')('sakura:vite')

export function serveViteDev(vite: ViteDevServer): Middleware {
const manifest = {}

return async (ctx) => {
const ssrContext = getSSRContext()

try {
const url = ctx.request.originalUrl

Expand All @@ -21,7 +24,11 @@ export function serveViteDev(vite: ViteDevServer): Middleware {
const render = (await vite.ssrLoadModule('/client/entry-server.ts'))
.render

const { html, preloadLinks, initialState } = await render(url, manifest)
const { html, preloadLinks, initialState } = await render(
url,
manifest,
ssrContext
)

const appHtml = renderHtml(template, { preloadLinks, html, initialState })

Expand Down Expand Up @@ -52,9 +59,15 @@ export function serveViteBuild(dist: string): Middleware[] {
return [
serveStatic,
async (ctx, next) => {
const ssrContext = await getSSRContext()

const url = ctx.request.url

const { html, preloadLinks, initialState } = await render(url, manifest)
const { html, preloadLinks, initialState } = await render(
url,
manifest,
ssrContext
)

const appHtml = renderHtml(indexTpl, { html, preloadLinks, initialState })

Expand All @@ -63,6 +76,12 @@ export function serveViteBuild(dist: string): Middleware[] {
]
}

async function getSSRContext() {
return {
isInstalled: await checkInstall()
}
}

function renderHtml(
template: string,
opt: {
Expand Down

0 comments on commit 257d137

Please sign in to comment.