Skip to content

Commit

Permalink
Merge pull request #141 from terwer/dev
Browse files Browse the repository at this point in the history
fix: 修复SSR报错问题
  • Loading branch information
terwer committed Jul 2, 2023
2 parents c54caae + 6dfcd58 commit a36b20b
Show file tree
Hide file tree
Showing 22 changed files with 181 additions and 113 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Based on the localization concept of siyuan-note , this plugin is natively share

The core idea of this plugin is: `Everything is a Page` . You can set a page to be the home page.

> Important: Version 1.8.0+ unifies the sharing logic in public sharing and authorization code mode, so you need to initialize the sharing type to avoid the old sharing link access exception, you only need to open the sharing pop-up window once, and it will be automatically initialized after opening.
[docs](https://blog.terwer.space/s/20230621001422-xsimx5v)

## Core Features
Expand Down
2 changes: 2 additions & 0 deletions README_zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

本插件的核心理念是:`一切皆页面` 。您可以设置某个页面为主页。

> 重要提示:1.8.0+ 版本对公共分享和授权码模式下的分享逻辑进行了统一处理,因此需要初始化分享类型,避免旧的分享链接访问异常,您只需要打开一次分享弹窗即可,打开之后会自动初始化。
[帮助文档](https://blog.terwer.space/s/20230621001422-xsimx5v)

## 核心特色
Expand Down
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 }
}
33 changes: 8 additions & 25 deletions composables/useCommonShareType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,40 +28,23 @@ 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 () => {
const shareTypeFetchFile = `/public/siyuan-blog/share-type.json`
const 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)) {
// 这时候需要再兼容历史数据
const id = (route.params.id ?? "") as string
const resPText = await fetchText(`/public/siyuan-blog/${id}.json`)
if (StrUtil.isEmptyString(resPText)) {
return ShareTypeEnum.ShareType_Public
} else {
return ShareTypeEnum.ShareType_Private
}
}

if (shareType.shareType === "private") {
return ShareTypeEnum.ShareType_Private
return ShareTypeEnum.ShareType_Public
}

return shareType.shareType
Expand All @@ -73,16 +56,16 @@ 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) ?? "{}"
await kernelApi.saveTextData(shareTypeFile, sJson)
logger.info("inited shareType in public dir", sJson)
logger.info("IMPORTANT NOTICE: inited shareType in public dir", sJson)
}

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
36 changes: 8 additions & 28 deletions nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const generateDynamicV = () => {
}

const isDev = process.env.NODE_ENV === "development"
const appBase = "/plugins/siyuan-blog/"
const appBase = "/"
const staticV = generateDynamicV()

// https://nuxt.com/docs/api/configuration/nuxt-config
Expand All @@ -28,14 +28,7 @@ export default defineNuxtConfig({
},

// build modules
modules: [
"@vueuse/nuxt",
"@nuxtjs/i18n-edge",
"@element-plus/nuxt",
"@nuxtjs/color-mode",
"@pinia/nuxt",
"@nuxt/image",
],
modules: ["@vueuse/nuxt", "@nuxtjs/i18n", "@element-plus/nuxt", "@nuxtjs/color-mode", "@pinia/nuxt", "@nuxt/image"],

// vueuse
vueuse: {
Expand All @@ -58,7 +51,7 @@ export default defineNuxtConfig({
define: {
"process.env.DEV_MODE": `"${isDev}"`,
"process.env.APP_BASE": `"${appBase}"`,
"process.env.SSR": `"false"`,
"process.env.SSR": `"true"`,
},
plugins: [],
},
Expand All @@ -69,14 +62,6 @@ export default defineNuxtConfig({
themes: ["dark"],
},

// https://nuxt.com/docs/guide/going-further/custom-routing#hash-mode-spa
ssr: false,
router: {
options: {
hashMode: true,
},
},

css: ["~/assets/siyuan/style.styl", "~/assets/siyuan/index.styl"],

app: {
Expand Down Expand Up @@ -113,17 +98,12 @@ export default defineNuxtConfig({

// 环境变量
runtimeConfig: {
// siyuanAuthToken: process.env.NUXT_SIYUAN_AUTH_TOKEN,
siyuanAuthToken: "",
// siyuanCookie: process.env.NUXT_SIYUAN_COOKIE,
siyuanCookie: "",
siyuanAuthToken: process.env.NUXT_SIYUAN_AUTH_TOKEN,
siyuanCookie: process.env.NUXT_SIYUAN_COOKIE,
public: {
// defaultType: process.env.NUXT_PUBLIC_DEFAULT_TYPE,
defaultType: "siyuan",
// siyuanApiUrl: process.env.NUXT_PUBLIC_SIYUAN_API_URL,
siyuanApiUrl: "",
// waitTime: process.env.NUXT_PUBLIC_WAIT_TIME,
waitTime: "0",
defaultType: process.env.NUXT_PUBLIC_DEFAULT_TYPE ?? "siyuan",
siyuanApiUrl: process.env.NUXT_PUBLIC_SIYUAN_API_URL ?? "http://127.0.0.1:6807",
waitTime: process.env.NUXT_PUBLIC_WAIT_TIME,
},
},
})
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.
Loading

1 comment on commit a36b20b

@vercel
Copy link

@vercel vercel bot commented on a36b20b Jul 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.