Skip to content

Commit

Permalink
feat: move client redirect feature into valaxy
Browse files Browse the repository at this point in the history
  • Loading branch information
yixiaojiu committed Jan 9, 2024
1 parent b035ccb commit 4442ea8
Show file tree
Hide file tree
Showing 15 changed files with 123 additions and 169 deletions.
1 change: 0 additions & 1 deletion demo/yun/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
"valaxy": "workspace:*",
"valaxy-addon-algolia": "workspace:*",
"valaxy-addon-bangumi": "workspace:*",
"valaxy-addon-client-redirects": "workspace:*",
"valaxy-addon-components": "workspace:*",
"valaxy-addon-lightgallery": "workspace:*",
"valaxy-addon-meting": "workspace:*",
Expand Down
7 changes: 7 additions & 0 deletions demo/yun/site.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,11 @@ export default defineSiteConfig({
encrypt: {
enable: true,
},

redirects: [
{
from: '/foo',
to: '/about',
},
],
})
9 changes: 0 additions & 9 deletions demo/yun/valaxy.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { addonLightGallery } from 'valaxy-addon-lightgallery'
import { addonMeting } from 'valaxy-addon-meting'
import { addonTest } from 'valaxy-addon-test'
import { addonWaline } from 'valaxy-addon-waline'
import { addonClientRedirects } from 'valaxy-addon-client-redirects'

// import { addonTwikoo } from 'valaxy-addon-twikoo'

Expand Down Expand Up @@ -128,13 +127,5 @@ export default defineValaxyConfig<ThemeConfig>({
// envId: 'https://twikoo.vercel.app',
// }),
addonTest(),
addonClientRedirects({
redirects: [
{
from: '/foo',
to: '/about',
},
],
}),
],
})
64 changes: 64 additions & 0 deletions docs/pages/guide/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,15 @@ password: your_password
:::

::: tip

<div lang="zh-CN">
如果在文章的 Front Matter 中设置了 `password`,文章中的部分加密将被忽略。
</div>

<div lang="en">
If you set `password` in Front Matter, partial encryption will be ignored.
</div>

:::

::: zh-CN
Expand All @@ -534,6 +542,62 @@ Wrap content to be encrypted in `<!-- valaxy-encrypt-start:your_password --><!--
Examples can be found in [Partial Content Encryption](/examples/partial-content-encryption)
:::

### 客户端重定向 {lang="zh-CN"}

### Client Redirects {lang="en"}

::: zh-CN
这会生成额外的 HTML 页面,用与跳转到 valaxy 中已有的页面。
:::

::: en
This will generate additional HTML pages, used to jump to the valaxy's existing pages.
:::

::: tip

<div lang="zh-CN">
客户端重定向只在 SSG build 时启用
</div>

<div lang="en">
Client redirects will only be enabled in SSG build
</div>

:::

::: zh-CN
例如:
:::

::: en
For example:
:::

```ts
// site.config.ts
export default defineSiteConfig({
redirects: [
{
from: ['/foo', '/bar'],
to: '/about',
},
{
from: '/v1/about',
to: '/about',
},
],
})
```

::: zh-CN
`/foo`, `/bar`, `/v1/about` 这些路由会被重定向到 `/about`
:::

::: en
`/foo`, `/bar`, `/v1/about` these routes will be redirected to `/about`
:::

## 主题配置 {lang="zh-CN"}

## Theme Config {lang="en"}
Expand Down
61 changes: 0 additions & 61 deletions packages/valaxy-addon-client-redirects/README.md

This file was deleted.

2 changes: 0 additions & 2 deletions packages/valaxy-addon-client-redirects/index.d.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/valaxy-addon-client-redirects/index.ts

This file was deleted.

35 changes: 0 additions & 35 deletions packages/valaxy-addon-client-redirects/node/index.ts

This file was deleted.

19 changes: 0 additions & 19 deletions packages/valaxy-addon-client-redirects/node/writeRedirectFiles.ts

This file was deleted.

19 changes: 0 additions & 19 deletions packages/valaxy-addon-client-redirects/package.json

This file was deleted.

8 changes: 0 additions & 8 deletions packages/valaxy-addon-client-redirects/types/index.ts

This file was deleted.

22 changes: 21 additions & 1 deletion packages/valaxy/node/build.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { resolve } from 'node:path'
import { join, resolve } from 'node:path'
import type { InlineConfig } from 'vite'
import { mergeConfig as mergeViteConfig, build as viteBuild } from 'vite'
import { build as viteSsgBuild } from 'vite-ssg/node'
Expand All @@ -8,6 +8,7 @@ import fs from 'fs-extra'
import consola from 'consola'
import type { ResolvedValaxyOptions } from './options'
import { ViteValaxyPlugins } from './plugins/preset'
import { collectRedirects, writeRedirectFiles } from './utils/clientRedirects'

export async function build(
options: ResolvedValaxyOptions,
Expand Down Expand Up @@ -55,6 +56,7 @@ export async function ssgBuild(

/**
* post process for ssg fix extra string like `/html>` `ml>` `l>`
* handle tasks after ssg build
* todo find why
* @param options
*/
Expand All @@ -72,4 +74,22 @@ export async function postProcessForSSG(options: ResolvedValaxyOptions) {
await fs.writeFile(indexPath, indexFile.slice(0, htmlTagStart + htmlTag.length), 'utf-8')
}
}

await generateClientRedirects(options)
}

export async function generateClientRedirects(options: ResolvedValaxyOptions) {
const outputPath = resolve(options.userRoot, 'dist')
const redirectRules = collectRedirects(options.config.siteConfig?.redirects ?? [])

const task = redirectRules.map(async (rule) => {
const fromPath = join(outputPath, `${rule.from}.html`)
const toPath = join(outputPath, `${rule.to}.html`)
const routeExist = await fs.pathExists(toPath)
if (!routeExist)
throw new Error(`the route of '${rule.to}' not exists`)
await writeRedirectFiles(rule.to, fromPath)
})

await Promise.all(task)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type { RedirectRule } from '../types'
import { writeFile } from 'node:fs/promises'
import { ensureFile } from 'fs-extra'
import type { RedirectRule } from '../../types'

function handleRoute(route: string) {
if (route === '/')
Expand Down Expand Up @@ -34,3 +36,20 @@ export function collectRedirects(redirectRule: RedirectRule[]) {

return redirects
}

export async function writeRedirectFiles(route: string, filePath: string) {
await ensureFile(filePath)
await writeFile(filePath, `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="refresh" content="0; url=${route}">
<link rel="canonical" href="${route}">
</head>
<script>
window.location.href = '${route}' + window.location.search + window.location.hash
</script>
</html>
`)
}
11 changes: 11 additions & 0 deletions packages/valaxy/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ export interface SocialLink {
color: string
}

export interface RedirectRule {
to: string
from: string | string[]
}

// shared with valaxy node and client
export interface SiteConfig {
/**
Expand Down Expand Up @@ -313,6 +318,12 @@ export interface SiteConfig {
* @description:zh-CN 限制代码块的高度,单位是 px
*/
codeHeightLimit?: number

/**
* @description:en-US client redirect rules
* @description:zh-CN 客户端重定向规则
*/
redirects?: RedirectRule[]
}

export type PartialDeep<T> = {
Expand Down
12 changes: 0 additions & 12 deletions pnpm-lock.yaml

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

0 comments on commit 4442ea8

Please sign in to comment.