Skip to content

Commit c4a0e55

Browse files
committed
fix: Solve the download button SSR hydration mismatch issue
1 parent 81f8783 commit c4a0e55

File tree

6 files changed

+51
-33
lines changed

6 files changed

+51
-33
lines changed

index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
66
<link rel="icon" href="/favicon_mac.ico" />
77
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
8+
<meta name="description" content="为编程初学者而生">
89
<title>Algo Bootstrap</title>
910
</head>
1011
<body>

src/client/components/download-button.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ import { ElMessage } from 'element-plus';
1313
},
1414
})
1515
export default class DownloadButton extends Vue {
16-
@Prop({ type: String, default: 'windows' }) platform!: string;
17-
@Prop({ type: String, default: 'x64' }) arch!: string;
16+
@Prop({ type: String, required: true }) platform!: string;
17+
@Prop({ type: String, required: true }) arch!: string;
1818
@Prop({ type: Boolean, default: false }) isHome: Boolean;
19-
@Prop({ type: String, default: '1.0.0-beta.1' }) version!: string;
19+
@Prop({ type: String, required: true }) version!: string;
2020
2121
get isSupportedPlatform(): boolean {
2222
return this.platform === 'windows' || this.platform === 'mac';

src/client/components/release-item.vue

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { ElMessage } from 'element-plus';
1515
})
1616
export default class ReleaseItem extends Vue {
1717
@Prop({ type: String, required: true }) platform!: string;
18-
@Prop({ type: String, default: '1.0.0-beta.1' }) version!: string;
18+
@Prop({ type: String, required: true }) version!: string;
1919
2020
get platformImage(): string {
2121
switch (this.platform) {
@@ -34,38 +34,39 @@ export default class ReleaseItem extends Vue {
3434
}
3535
3636
get downloadLink(): string {
37-
const version = this.version || '1.0.0-beta.1';
38-
const downloadLink = new ReleasesConfig(version).downloadSingleSystemLink(this.platform, this.defaultArch);
37+
const downloadLink = new ReleasesConfig(this.version).downloadSingleSystemLink(
38+
this.platform,
39+
this.defaultArch,
40+
);
3941
return downloadLink;
4042
}
4143
4244
get otherLinks(): Array<{ arch: string; name?: string; link: string }> {
43-
const version = this.version || '1.0.0-beta.1';
4445
switch (this.platform) {
4546
case 'windows':
4647
return [
4748
{
4849
arch: 'x64',
4950
name: 'Windows (x64)',
50-
link: new ReleasesConfig(version).downloadSingleSystemLink('windows', 'x64'),
51+
link: new ReleasesConfig(this.version).downloadSingleSystemLink('windows', 'x64'),
5152
},
5253
{
5354
arch: 'Arm64',
5455
name: 'Windows (Arm64)',
55-
link: new ReleasesConfig(version).downloadSingleSystemLink('windows', 'arm64'),
56+
link: new ReleasesConfig(this.version).downloadSingleSystemLink('windows', 'arm64'),
5657
},
5758
];
5859
case 'mac':
5960
return [
6061
{
6162
arch: 'Intel Chip',
6263
name: 'macOS (Intel Chip)',
63-
link: new ReleasesConfig(version).downloadSingleSystemLink('mac', 'x64'),
64+
link: new ReleasesConfig(this.version).downloadSingleSystemLink('mac', 'x64'),
6465
},
6566
{
6667
arch: 'Apple Silicon',
6768
name: 'macOS (Apple Silicon)',
68-
link: new ReleasesConfig(version).downloadSingleSystemLink('mac', 'arm64'),
69+
link: new ReleasesConfig(this.version).downloadSingleSystemLink('mac', 'arm64'),
6970
},
7071
];
7172
default:
@@ -75,12 +76,11 @@ export default class ReleaseItem extends Vue {
7576
7677
// SSR 安全的描述文本
7778
get staticAsideDesc(): string | null {
78-
const version = this.version || '1.0.0-beta.1';
7979
switch (this.platform) {
8080
case 'windows':
81-
return `version ${version} for x64`;
81+
return `version ${this.version} for x64`;
8282
case 'mac':
83-
return `version ${version} for Intel Chip`;
83+
return `version ${this.version} for Intel Chip`;
8484
default:
8585
return null;
8686
}
@@ -98,15 +98,14 @@ export default class ReleaseItem extends Vue {
9898
9999
get dynamicAsideDesc(): string | null {
100100
const arch = this.clientArch;
101-
const version = this.version || '1.0.0-beta.1';
102101
switch (this.platform) {
103102
case 'windows':
104-
return `version ${version} for ${arch == 'arm64' ? 'Arm64' : arch}`;
103+
return `version ${this.version} for ${arch == 'arm64' ? 'Arm64' : arch}`;
105104
case 'mac':
106105
if (arch == 'arm64') {
107-
return `version ${version} for Apple Silicon`;
106+
return `version ${this.version} for Apple Silicon`;
108107
} else {
109-
return `version ${version} for Intel Chip`;
108+
return `version ${this.version} for Intel Chip`;
110109
}
111110
default:
112111
return null;
@@ -134,9 +133,9 @@ export default class ReleaseItem extends Vue {
134133
</client-only>
135134
<main>
136135
<ClientOnly>
137-
<DownloadButton :platform="this.platform" :arch="this.arch" />
136+
<DownloadButton :platform="this.platform" :arch="this.arch" :version="this.version" />
138137
<template #fallback>
139-
<DownloadButton :platform="this.platform" :arch="defaultArch" />
138+
<DownloadButton :platform="this.platform" :arch="defaultArch" :version="this.version" />
140139
</template>
141140
</ClientOnly>
142141
</main>

src/client/modules/home/home-display.vue

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ import { DataConfig } from '@client/utils/data.config';
1212
})
1313
export default class HomeDisplay extends Vue {
1414
@Prop({ type: String, required: true }) readonly platform!: string;
15-
@Prop({ type: String, default: 'arm64' }) readonly arch!: string;
15+
@Prop({ type: String, required: true }) readonly arch!: string;
1616
@Prop({ type: Boolean, default: false }) readonly isMobile!: boolean;
1717
@Prop({ type: Boolean, default: false }) readonly isClientMounted!: boolean;
18-
@Prop({ type: String, default: '' }) readonly version!: string;
19-
@Prop({ type: String, default: '' }) readonly releasesTime!: string;
18+
@Prop({ type: String, required: true }) readonly version!: string;
19+
@Prop({ type: String, required: true }) readonly releasesTime!: string;
2020
isSupportedPlatform = false;
2121
isStartOpen = false;
2222
@@ -145,7 +145,7 @@ export default class HomeDisplay extends Vue {
145145
<h2>一键配置现代的 C++、Python 和 VS Code 编程环境</h2>
146146
</header>
147147
<div class="content-main-subtitle">
148-
<DownloadButton :platform="platform" :is-home="true" :arch="arch" />
148+
<DownloadButton :platform="platform" :is-home="true" :arch="arch" :version="version" />
149149
<div class="start-dropdown" @mouseenter="handleStartMouseEnter" @mouseleave="handleStartMouseLeave">
150150
<div class="start" :href="getLinks().docs" target="_blank" @click="handleStartClick">
151151
<!-- <svg

src/client/modules/home/home.view.vue

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import { GetReleasesDTO } from '@common/modules/releases/releases.dto';
2828
@RenderMethod(RenderMethodKind.SSR)
2929
export default class Home extends Vue {
3030
homeState: GetReleasesDTO = {
31-
version: '',
31+
version: '1.0.0-beta.1', // 默认版本,避免空字符串
3232
releaseDate: '',
3333
url: '',
3434
releasesInfo: {} as any,
@@ -71,11 +71,14 @@ export default class Home extends Vue {
7171
beforeDestroy() {
7272
window.removeEventListener('resize', this.handleResize);
7373
}
74-
created() {
74+
async created() {
75+
this.loadVersionData();
76+
}
77+
78+
async loadVersionData() {
7579
try {
76-
axios.get('https://cdn.algoux.cn/algo-bootstrap/version.json').then((response) => {
77-
this.homeState = response.data;
78-
});
80+
const response = await axios.get('https://cdn.algoux.cn/algo-bootstrap/version.json?t=' + Date.now());
81+
this.homeState = response.data;
7982
} catch (error) {
8083
console.error('Failed to load home view data:', error);
8184
this.homeState = this.homeState; // 使用默认值

src/client/modules/releases/releases.view.vue

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { GetReleasesDTO } from '@common/modules/releases/releases.dto';
1717
@RenderMethod(RenderMethodKind.SSR)
1818
export default class Releases extends Vue {
1919
releasesState: GetReleasesDTO = {
20-
version: '',
20+
version: '1.0.0-beta.1', // 默认版本,避免空字符串
2121
url: '',
2222
releaseDate: '',
2323
releasesInfo: {} as any,
@@ -27,12 +27,27 @@ export default class Releases extends Vue {
2727
return DataConfig.GITHUB_RELEASES;
2828
}
2929
30-
async created() {
30+
async asyncData() {
3131
try {
32-
const response = await axios.get('https://cdn.algoux.cn/algo-bootstrap/version.json');
32+
const response = await axios.get('https://cdn.algoux.cn/algo-bootstrap/version.json?t=' + Date.now());
33+
return {
34+
releasesState: response.data,
35+
}
36+
} catch (error) {
37+
console.error('Failed to load release data in asyncData:', error);
38+
return {
39+
releasesState: this.releasesState, // 使用默认值
40+
}
41+
}
42+
}
43+
44+
async mounted() {
45+
// 客户端也加载数据,确保数据同步
46+
try {
47+
const response = await axios.get('https://cdn.algoux.cn/algo-bootstrap/version.json?t=' + Date.now());
3348
this.releasesState = response.data;
3449
} catch (error) {
35-
console.error('Failed to fetch latest release info:', error);
50+
console.error('Failed to load release data in mounted:', error);
3651
}
3752
}
3853
}

0 commit comments

Comments
 (0)