Commit 047b7e03 authored by AI Bot's avatar AI Bot

支持单语言站点构建:SITE_LANG=zh/en 各出一份静态站,独立域名部署

- next.config.js 注入 SITE_LANG 构建期环境变量
- 新增 utils/siteLang.js;_app.js 单语言模式跳过 localStorage/?lang= 逻辑,语言在 SSR 阶段固定(标题/正文直接烘进 HTML)
- Header/Footer 语言切换按钮在单语言模式下改为跳转对方域名同路径(域名配置在 head.md 的 siteZH/siteEN,CMS 后台可编辑)
- config.yml 补充 siteZH/siteEN 表单字段,CMS 后台不变仍是单一入口
- package.json 新增 build:zh / build:en 脚本
Co-Authored-By: 's avatarClaude Code <noreply@anthropic.com>
parent 74375175
...@@ -2,13 +2,23 @@ import React from 'react' ...@@ -2,13 +2,23 @@ import React from 'react'
import classnames from 'classnames' import classnames from 'classnames'
// eslint-disable-next-line no-unused-vars // eslint-disable-next-line no-unused-vars
import { ThemeContext } from '../../pages/_app.js' import { ThemeContext } from '../../pages/_app.js'
import { IS_SINGLE_LANG } from '../../utils/siteLang'
import footer from '../../content/foot.md' import footer from '../../content/foot.md'
import head from '../../content/head.md'
import './index.css' import './index.css'
import './595.css' import './595.css'
import './830.css' import './830.css'
import './1440.css' import './1440.css'
import './1440+.css' import './1440+.css'
// 单语言模式下跳转到对方语言站点(同路径同参数)
function jumpToSite(targetEn) {
const { siteZH, siteEN } = head.attributes
const base = (targetEn ? siteEN : siteZH) || ''
window.location.href =
base + window.location.pathname + window.location.search
}
export default class Footer extends React.Component { export default class Footer extends React.Component {
genContactRow(left, right) { genContactRow(left, right) {
return ( return (
...@@ -81,6 +91,10 @@ export default class Footer extends React.Component { ...@@ -81,6 +91,10 @@ export default class Footer extends React.Component {
<span <span
onClick={(e) => { onClick={(e) => {
e.stopPropagation() e.stopPropagation()
if (IS_SINGLE_LANG) {
jumpToSite(false)
return
}
setLang(false) setLang(false)
}} }}
className={classnames({ className={classnames({
...@@ -94,6 +108,10 @@ export default class Footer extends React.Component { ...@@ -94,6 +108,10 @@ export default class Footer extends React.Component {
<span <span
onClick={(e) => { onClick={(e) => {
e.stopPropagation() e.stopPropagation()
if (IS_SINGLE_LANG) {
jumpToSite(true)
return
}
setLang(true) setLang(true)
}} }}
className={classnames({ className={classnames({
......
...@@ -3,11 +3,20 @@ import Link from 'next/link' ...@@ -3,11 +3,20 @@ import Link from 'next/link'
import Head from 'next/head' import Head from 'next/head'
import classnames from 'classnames' import classnames from 'classnames'
import { ThemeDecorator } from '../../pages/_app.js' import { ThemeDecorator } from '../../pages/_app.js'
import { IS_SINGLE_LANG } from '../../utils/siteLang'
import head from '../../content/head.md' import head from '../../content/head.md'
// import footer from '../../content/foot.md' // import footer from '../../content/foot.md'
import Modal from '../Modal' import Modal from '../Modal'
import './index.css' import './index.css'
// 单语言模式下跳转到对方语言站点(同路径同参数)
function jumpToSite(targetEn) {
const { siteZH, siteEN } = head.attributes
const base = (targetEn ? siteEN : siteZH) || ''
window.location.href =
base + window.location.pathname + window.location.search
}
export class Header extends React.PureComponent { export class Header extends React.PureComponent {
constructor(props) { constructor(props) {
super(props) super(props)
...@@ -97,6 +106,10 @@ export class Header extends React.PureComponent { ...@@ -97,6 +106,10 @@ export class Header extends React.PureComponent {
<span <span
onClick={(e) => { onClick={(e) => {
e.stopPropagation() e.stopPropagation()
if (IS_SINGLE_LANG) {
jumpToSite(false)
return
}
setLang(false) setLang(false)
this.openModal(null, e) this.openModal(null, e)
}} }}
...@@ -112,6 +125,10 @@ export class Header extends React.PureComponent { ...@@ -112,6 +125,10 @@ export class Header extends React.PureComponent {
<span <span
onClick={(e) => { onClick={(e) => {
e.stopPropagation() e.stopPropagation()
if (IS_SINGLE_LANG) {
jumpToSite(true)
return
}
setLang(true) setLang(true)
this.openModal(null, e) this.openModal(null, e)
}} }}
...@@ -184,6 +201,10 @@ export class Header extends React.PureComponent { ...@@ -184,6 +201,10 @@ export class Header extends React.PureComponent {
'header-index': true 'header-index': true
})} })}
onClick={() => { onClick={() => {
if (IS_SINGLE_LANG) {
jumpToSite(!en)
return
}
setLang(en ? false : true) setLang(en ? false : true)
}} }}
> >
......
--- ---
# 单语言模式下,语言切换按钮跳转的对方站点域名(带 https://,不带结尾斜杠)
siteZH: ''
siteEN: ''
home: 首页 home: 首页
homeEN: Home homeEN: Home
about: 关于我们 about: 关于我们
......
...@@ -4,6 +4,10 @@ const RemoveStrictPlugin = require('remove-strict-webpack-plugin') ...@@ -4,6 +4,10 @@ const RemoveStrictPlugin = require('remove-strict-webpack-plugin')
// const exec = require('child_process').exec // const exec = require('child_process').exec
module.exports = withCss({ module.exports = withCss({
// 构建期固定站点语言:'zh' / 'en',见 utils/siteLang.js
env: {
SITE_LANG: process.env.SITE_LANG || ''
},
webpack: (cfg) => { webpack: (cfg) => {
cfg.module.rules.push({ cfg.module.rules.push({
test: /\.md$/, test: /\.md$/,
......
...@@ -6,6 +6,8 @@ ...@@ -6,6 +6,8 @@
"scripts": { "scripts": {
"dev": "set NODE_OPTIONS=--openssl-legacy-provider && next", "dev": "set NODE_OPTIONS=--openssl-legacy-provider && next",
"build": "node scripts/build-timeline-font.js && set NODE_OPTIONS=--openssl-legacy-provider && next build", "build": "node scripts/build-timeline-font.js && set NODE_OPTIONS=--openssl-legacy-provider && next build",
"build:zh": "node scripts/build-timeline-font.js && set NODE_OPTIONS=--openssl-legacy-provider && set SITE_LANG=zh&& next build",
"build:en": "node scripts/build-timeline-font.js && set NODE_OPTIONS=--openssl-legacy-provider && set SITE_LANG=en&& next build",
"start": "set NODE_OPTIONS=--openssl-legacy-provider && next start", "start": "set NODE_OPTIONS=--openssl-legacy-provider && next start",
"exportwatch": "nodemon -e js,css,json,md,png,jpg,svg --exec \"npm run export\" --ignore .next/ --ignore out/", "exportwatch": "nodemon -e js,css,json,md,png,jpg,svg --exec \"npm run export\" --ignore .next/ --ignore out/",
"export": "npm run build && next export", "export": "npm run build && next export",
......
...@@ -3,6 +3,7 @@ import App, { Container } from 'next/app' ...@@ -3,6 +3,7 @@ import App, { Container } from 'next/app'
import classnames from 'classnames' import classnames from 'classnames'
// import * as Sentry from '@sentry/browser' // import * as Sentry from '@sentry/browser'
import initReactFastclick from 'react-fastclick' import initReactFastclick from 'react-fastclick'
import { IS_SINGLE_LANG, FORCE_EN } from '../utils/siteLang'
const ThemeContext = React.createContext('theme') const ThemeContext = React.createContext('theme')
// Sentry.init({ // Sentry.init({
...@@ -45,7 +46,7 @@ class MyApp extends App { ...@@ -45,7 +46,7 @@ class MyApp extends App {
constructor(...props) { constructor(...props) {
super(...props) super(...props)
this.ctx = { this.ctx = {
en: null, en: IS_SINGLE_LANG ? FORCE_EN : null,
setLang: (isEnglish, set = true) => { setLang: (isEnglish, set = true) => {
if (this.ctx.en === isEnglish) { if (this.ctx.en === isEnglish) {
return return
...@@ -70,6 +71,10 @@ class MyApp extends App { ...@@ -70,6 +71,10 @@ class MyApp extends App {
// super.componentDidCatch(error, errorInfo) // super.componentDidCatch(error, errorInfo)
// } // }
componentDidMount() { componentDidMount() {
// 单语言站点语言由构建期固定,不读 localStorage / ?lang=
if (IS_SINGLE_LANG) {
return
}
// window.location.href. // window.location.href.
var lan = getQueryVariable("lang"); var lan = getQueryVariable("lang");
if(lan) { if(lan) {
......
...@@ -376,6 +376,8 @@ collections: ...@@ -376,6 +376,8 @@ collections:
name: "head" name: "head"
file: "content/head.md" file: "content/head.md"
fields: fields:
- { label: "中文站域名(语言切换跳转用,带 https:// 不带结尾斜杠)", name: "siteZH", widget: "string", required: false }
- { label: "英文站域名(语言切换跳转用,带 https:// 不带结尾斜杠)", name: "siteEN", widget: "string", required: false }
- { label: "Home", name: "home", widget: "string" } - { label: "Home", name: "home", widget: "string" }
- { label: "HomeEN", name: "homeEN", widget: "string" } - { label: "HomeEN", name: "homeEN", widget: "string" }
- { label: "About", name: "about", widget: "string" } - { label: "About", name: "about", widget: "string" }
......
// 单语言站点构建开关:由构建时的环境变量 SITE_LANG 决定
// SITE_LANG=zh → 只出中文站(部署到中文域名)
// SITE_LANG=en → 只出英文站(部署到英文域名)
// 不设置 → 双语模式(dev 用,站内 toggle 切换,行为同旧版)
export const SITE_LANG = (process.env.SITE_LANG || '').toLowerCase()
export const IS_SINGLE_LANG = SITE_LANG === 'zh' || SITE_LANG === 'en'
export const FORCE_EN = SITE_LANG === 'en'
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment