Skip to content

Commit

Permalink
fix: 修复授权码分享模式SSR报错问题
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Jul 2, 2023
1 parent 7c70974 commit 6dfcd58
Show file tree
Hide file tree
Showing 20 changed files with 168 additions and 81 deletions.
8 changes: 2 additions & 6 deletions components/default/Home.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,9 @@ const props = defineProps({
</script>

<template>
<el-container>
<default-header />
<el-main>

<default-detail :page-id="props.pageId" :override-seo="true" />
</el-main>
<default-footer />
</el-container>

</template>

<style scoped></style>
22 changes: 14 additions & 8 deletions components/default/HomePage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,20 @@ const goSetting = async () => {
</script>

<template>
<div v-if="StrUtil.isEmptyString(homePageId)">
<el-empty :description="t('blog.index.no.home')">
<el-button type="primary" @click="goSetting">{{ t("blog.index.goto.set.home") }}</el-button>
</el-empty>
</div>
<div v-else>
<default-home :page-id="homePageId" />
</div>
<el-container>
<default-header />
<el-main>
<div v-if="StrUtil.isEmptyString(homePageId)">
<el-empty :description="t('blog.index.no.home')">
<el-button type="primary" @click="goSetting">{{ t("blog.index.goto.set.home") }}</el-button>
</el-empty>
</div>
<div v-else>
<default-home :page-id="homePageId" />
</div>
</el-main>
<default-footer />
</el-container>
</template>

<style scoped></style>
11 changes: 6 additions & 5 deletions components/static/Detail.vue
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { Post } from "zhi-blog-api"
import { createAppLogger } from "~/common/appLogger"
import { getSummery } from "~/utils/utils"
import { useServerAssets } from "~/plugins/renderer/useServerAssets"
import { useAuthModeFetch } from "~/composables/useAuthModeFetch"
// https://github.com/nuxt/nuxt/issues/15346
// 由于布局是个宏,静态构建情况下,不能动态设置,只能在前面的页面写死
Expand All @@ -48,15 +49,15 @@ const { t } = useI18n()
const route = useRoute()
const id = props.pageId ?? ((route.params.id ?? "") as string)
const { getFirstImageSrc } = useServerAssets()
const { fetchPublicText } = useAuthModeFetch()
// datas
const getPostData = async () => {
const mdResponse = await fetch(`/public/siyuan-blog/${id}.json`)
const mdText = await mdResponse.text()
formData.post = JsonUtil.safeParse<Post>(mdText, {} as Post)
const resText = await fetchPublicText(`${id}.json`)
formData.post = JsonUtil.safeParse<Post>(resText, {} as Post)
formData.shareEnabled = !ObjectUtil.isEmptyObject(formData.post)
logger.info("post=>", formData.post)
logger.info(`shareEnabled=>${formData.shareEnabled}`)
// logger.info("post=>", formData.post)
// logger.info(`shareEnabled=>${formData.shareEnabled}`)
}
const formData = reactive({
Expand Down
8 changes: 1 addition & 7 deletions components/static/Home.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,5 @@ const props = defineProps({
</script>

<template>
<el-container>
<static-header />
<el-main>
<static-detail :page-id="props.pageId" :override-seo="true" />
</el-main>
<static-footer />
</el-container>
<static-detail :page-id="props.pageId" :override-seo="true" />
</template>
27 changes: 14 additions & 13 deletions components/static/HomePage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,23 @@ const seoMeta = {
useSeoMeta(seoMeta)
const homePageId = setting?.homePageId ?? undefined
// methods
const goSetting = async () => {
await navigateTo("/setting")
}
</script>

<template>
<div v-if="StrUtil.isEmptyString(homePageId)">
<el-empty :description="t('blog.index.no.home')">
<el-button type="primary" @click="goSetting">{{ t("blog.index.goto.set.home") }}</el-button>
</el-empty>
</div>
<div v-else>
<static-home :page-id="homePageId" />
</div>
<el-container>
<static-header />
<el-main>
<div v-if="StrUtil.isEmptyString(homePageId)">
<el-empty :description="t('blog.index.no.home')">
<el-alert type="warning" :description="t('blog.index.goto.set.home.static')" :closable="false"></el-alert>
</el-empty>
</div>
<div v-else>
<static-home :page-id="homePageId" />
</div>
</el-main>
<static-footer />
</el-container>
</template>

<style lang="stylus">
Expand Down
47 changes: 47 additions & 0 deletions composables/useAuthModeFetch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2023, Terwer . All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Terwer designates this
* particular file as subject to the "Classpath" exception as provided
* by Terwer in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Terwer, Shenzhen, Guangdong, China, youweics@163.com
* or visit www.terwer.space if you need additional information or have any
* questions.
*/

import { createAppLogger } from "~/common/appLogger"
import { useSiyuanApi } from "~/composables/api/useSiyuanApi"

export const useAuthModeFetch = () => {
const logger = createAppLogger("use-auth-mode-fetch")
const { kernelApi } = useSiyuanApi()

/**
* 获取文本
*
* @param filename - 获取相对于 public/siyuan-blog 目录的文本
*/
const fetchPublicText = async (filename: string) => {
const shareTypeFetchFile = `/public/siyuan-blog/${filename}`
logger.info("getPublicFile in auth mode", shareTypeFetchFile)
const resText = await kernelApi.getPublicFile(shareTypeFetchFile)
logger.debug("get text from auth mode", resText)
return resText
}

return { fetchPublicText }
}
26 changes: 6 additions & 20 deletions composables/useCommonShareType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,33 +28,19 @@ import { ShareType } from "~/models/ShareType"
import { ShareTypeEnum } from "~/enums/ShareTypeEnum"
import { createAppLogger } from "~/common/appLogger"
import { useSiyuanApi } from "~/composables/api/useSiyuanApi"
import { useAuthModeFetch } from "~/composables/useAuthModeFetch"

export const useCommonShareType = () => {
const logger = createAppLogger("use-common-share-type")
const { kernelApi } = useSiyuanApi()
const route = useRoute()

const fetchText = async (fileUrl: string) => {
const res = await fetch(fileUrl)
return await res.text()
}
const { fetchPublicText } = useAuthModeFetch()
const shareTypeJsonFile = "share-type.json"

/**
* 获取分享类型
*/
const getShareType = async () => {
let resText
if (process.env.SSR === "true") {
logger.warn("SSR mode, using kernel api for fetching")
const kernelShareTypeFile = `/data/public/siyuan-blog/share-type.json`
resText = await kernelApi.getFile(kernelShareTypeFile, "text")
console.log("get shareType text from kernel=>", resText)
} else {
const shareTypeFetchFile = `/public/siyuan-blog/share-type.json`
resText = await fetchText(shareTypeFetchFile)
logger.info("get shareType text from store", resText)
}

const resText = await fetchPublicText(shareTypeJsonFile)
const shareType = JsonUtil.safeParse(resText, {} as any)
logger.info("get shareType from store", shareType)
if (ObjectUtil.isEmptyObject(shareType)) {
Expand All @@ -70,7 +56,7 @@ export const useCommonShareType = () => {
}

const updateShareType = async (shareType: ShareTypeEnum) => {
const shareTypeFile = `/data/public/siyuan-blog/share-type.json`
const shareTypeFile = `/data/public/siyuan-blog/${shareTypeJsonFile}`
const sType = new ShareType()
sType.shareType = shareType
const sJson = JSON.stringify(sType) ?? "{}"
Expand All @@ -79,7 +65,7 @@ export const useCommonShareType = () => {
}

const removeShareType = async () => {
const shareTypeFile = `/data/public/siyuan-blog/share-type.json`
const shareTypeFile = `/data/public/siyuan-blog/${shareTypeJsonFile}`
await kernelApi.removeFile(shareTypeFile)
}

Expand Down
3 changes: 2 additions & 1 deletion locales/en_US.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,14 @@ export default {
"switch.unactive.text": "Normal",
"blog.index.no.home": "Oh, you haven't set up your homepage yet!",
"blog.index.goto.set.home": "Go to set up my homepage now",
"blog.index.goto.set.home.static": "",
"blog.share": "Share to web",
"blog.share.options": "Share options",
"blog.share.setting": "Share setting",
"blog.site.url.label": "Custom domain",
"blog.site.url": "Custom domain",
"blog.site.url.placeholder":
"Please fill in the complete address (if the port, you need to include the port), make sure that the domain name has been resolved, for example: http://siyuan.terwergreen.com:6808",
"Please fill in the complete address (if the port, you need to include the port), make sure that theThe authorization code has been enabled, there is no permission to modify, please set the homepage on the sharing interface domain name has been resolved, for example: http://siyuan.terwergreen.com:6808",
"blog.site.title.label": "Home title",
"blog.site.title": "Shallow sea shellfish",
"blog.site.slogan.label": "Home subtitle",
Expand Down
1 change: 1 addition & 0 deletions locales/zh_CN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export default {
"switch.unactive.text": "正常模式",
"blog.index.no.home": "啊哦,您还没设置自己的主页哟!",
"blog.index.goto.set.home": "马上去设置我的主页",
"blog.index.goto.set.home.static": "授权码已开启,暂无权限修改,请在分享界面设置主页",
"blog.share": "在线分享",
"blog.share.options": "分享选项",
"blog.share.setting": "分享设置",
Expand Down
2 changes: 1 addition & 1 deletion nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export default defineNuxtConfig({
siyuanCookie: process.env.NUXT_SIYUAN_COOKIE,
public: {
defaultType: process.env.NUXT_PUBLIC_DEFAULT_TYPE ?? "siyuan",
siyuanApiUrl: process.env.NUXT_PUBLIC_SIYUAN_API_URL ?? "http://127.0.0.1:6806",
siyuanApiUrl: process.env.NUXT_PUBLIC_SIYUAN_API_URL ?? "http://127.0.0.1:6807",
waitTime: process.env.NUXT_PUBLIC_WAIT_TIME,
},
},
Expand Down
2 changes: 1 addition & 1 deletion nuxt.node.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export default defineNuxtConfig({
siyuanCookie: process.env.NUXT_SIYUAN_COOKIE,
public: {
defaultType: process.env.NUXT_PUBLIC_DEFAULT_TYPE ?? "siyuan",
siyuanApiUrl: process.env.NUXT_PUBLIC_SIYUAN_API_URL ?? "http://127.0.0.1:6806",
siyuanApiUrl: process.env.NUXT_PUBLIC_SIYUAN_API_URL ?? "http://127.0.0.1:6807",
waitTime: process.env.NUXT_PUBLIC_WAIT_TIME,
},
},
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@
"highlight.js": "^11.8.0",
"pinia": "^2.1.4",
"zhi-device": "^2.3.0",
"zhi-siyuan-api": "^1.25.6"
"zhi-siyuan-api": "^1.26.0"
}
}
File renamed without changes.
File renamed without changes.
29 changes: 29 additions & 0 deletions plugins/06.renderer.static.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2023, Terwer . All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Terwer designates this
* particular file as subject to the "Classpath" exception as provided
* by Terwer in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Terwer, Shenzhen, Guangdong, China, youweics@163.com
* or visit www.terwer.space if you need additional information or have any
* questions.
*/

// stub - block nuxt getSSRProps error
export default defineNuxtPlugin(({ vueApp }) => {
vueApp.directive("sbeauty", {})
})
File renamed without changes.
File renamed without changes.
29 changes: 29 additions & 0 deletions plugins/10.domparser.static.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2023, Terwer . All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Terwer designates this
* particular file as subject to the "Classpath" exception as provided
* by Terwer in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Terwer, Shenzhen, Guangdong, China, youweics@163.com
* or visit www.terwer.space if you need additional information or have any
* questions.
*/

// stub - block nuxt getSSRProps error
export default defineNuxtPlugin(({ vueApp }) => {
vueApp.directive("sdomparser", {})
})
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6dfcd58

Please sign in to comment.