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 6c0c651 commit 7c70974
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 45 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
31 changes: 14 additions & 17 deletions composables/useCommonShareType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,22 @@ export const useCommonShareType = () => {
* 获取分享类型
*/
const getShareType = async () => {
const shareTypeFetchFile = `/public/siyuan-blog/share-type.json`
const resText = await fetchText(shareTypeFetchFile)
logger.info("get shareType text from store", resText)
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 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 @@ -78,7 +75,7 @@ export const useCommonShareType = () => {
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 () => {
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:6806",
waitTime: process.env.NUXT_PUBLIC_WAIT_TIME,
},
},
})

0 comments on commit 7c70974

Please sign in to comment.