Skip to content

Commit 68a2416

Browse files
committed
feat: 更新 version 版本号注入方式,角色 CSP 问题
fix #76
1 parent 489c669 commit 68a2416

File tree

7 files changed

+30
-24
lines changed

7 files changed

+30
-24
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
.idea
44
*.log
55
*.tgz
6+
.turbo
67
coverage
78
dist
89
lib-cov

.npmrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
ignore-workspace-root-check=true
2-
registry=https://registry.npmjs.org
2+
registry=https://registry.npmjs.org
3+
strict-peer-dependencies=false

packages/core/src/constant.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export const JSON_FILE_NAME = 'web_version_by_plugin'
33
export const INJECT_STYLE_FILE_NAME = 'webUpdateNoticeInjectStyle'
44
/** .global is iife suffix */
55
export const INJECT_SCRIPT_FILE_NAME = 'webUpdateNoticeInjectScript.global'
6+
export const INJECT_SCRIPT_TAG_ID = '_pwun_'
67
export const CUSTOM_UPDATE_EVENT_NAME = 'plugin_web_update_notice'
78
export const NOTIFICATION_ANCHOR_CLASS_NAME = 'plugin-web-update-notice-anchor'
89

packages/core/src/injectScript.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { LocaleData, Options, VersionJSON } from './type'
22
import {
33
CUSTOM_UPDATE_EVENT_NAME,
44
DIRECTORY_NAME,
5+
INJECT_SCRIPT_TAG_ID,
56
JSON_FILE_NAME,
67
LOCAL_STORAGE_PREFIX,
78
NOTIFICATION_ANCHOR_CLASS_NAME,
@@ -64,6 +65,21 @@ window.pluginWebUpdateNotice_ = {
6465
},
6566
}
6667

68+
/**
69+
* Get the version from the script tag
70+
*/
71+
function getLocaleVersion() {
72+
const script = document.querySelector(`script[data-id="${INJECT_SCRIPT_TAG_ID}"]`)
73+
if (!script)
74+
return ''
75+
const version = script.getAttribute('data-v')
76+
if (!version)
77+
return ''
78+
79+
window.pluginWebUpdateNotice_version = version
80+
return version
81+
}
82+
6783
/**
6884
* It checks whether the system has been updated and if so, it shows a notification.
6985
* @param {Options} options - Options
@@ -78,6 +94,7 @@ function __checkUpdateSetup__(options: Options) {
7894
checkOnLoadFileError = true,
7995
} = options
8096
const checkSystemUpdate = () => {
97+
const localeVersion = getLocaleVersion()
8198
window
8299
.fetch(`${injectFileBase}${DIRECTORY_NAME}/${JSON_FILE_NAME}.json?t=${Date.now()}`)
83100
.then((response) => {
@@ -90,7 +107,7 @@ function __checkUpdateSetup__(options: Options) {
90107
if (silence)
91108
return
92109
latestVersion = versionFromServer
93-
if (window.pluginWebUpdateNotice_version !== versionFromServer) {
110+
if (localeVersion !== versionFromServer) {
94111
// dispatch custom event
95112
document.body.dispatchEvent(new CustomEvent(CUSTOM_UPDATE_EVENT_NAME, {
96113
detail: {

packages/umi-plugin/src/index.ts

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { Options } from '@plugin-web-update-notification/core'
55
import {
66
DIRECTORY_NAME,
77
INJECT_SCRIPT_FILE_NAME,
8+
INJECT_SCRIPT_TAG_ID,
89
INJECT_STYLE_FILE_NAME,
910
JSON_FILE_NAME,
1011
NOTIFICATION_ANCHOR_CLASS_NAME,
@@ -17,10 +18,6 @@ import { name as pkgName } from '../package.json'
1718

1819
export type { Options } from '@plugin-web-update-notification/core'
1920

20-
const injectVersionTpl = (version: string) => {
21-
return `window.pluginWebUpdateNotice_version = '${version}';`
22-
}
23-
2421
export default (api: IApi) => {
2522
api.describe({
2623
key: 'webUpdateNotification',
@@ -103,17 +100,6 @@ export default (api: IApi) => {
103100
]
104101
})
105102

106-
api.addHTMLHeadScripts(() => {
107-
const scriptList = []
108-
scriptList.push({
109-
content: injectVersionTpl(version),
110-
})
111-
scriptList.push({
112-
src: `${injectFileBase}${DIRECTORY_NAME}/${INJECT_SCRIPT_FILE_NAME}.${jsFileHash}.js`,
113-
})
114-
return scriptList
115-
})
116-
117103
api.onBuildComplete(() => {
118104
const outputPath = resolve(api.userConfig.outputPath || 'dist')
119105
mkdirSync(`${outputPath}/${DIRECTORY_NAME}`)
@@ -131,6 +117,9 @@ export default (api: IApi) => {
131117
api.modifyHTML(($) => {
132118
if (!hiddenDefaultNotification)
133119
$('body').append(`<div class="${NOTIFICATION_ANCHOR_CLASS_NAME}"></div>`)
120+
121+
$('head').prepend(`<script data-id="${INJECT_SCRIPT_TAG_ID}" data-v="${version}" src="${injectFileBase}${DIRECTORY_NAME}/${INJECT_SCRIPT_FILE_NAME}.${jsFileHash}.js"></script>`)
122+
134123
return $
135124
})
136125
}

packages/vite-plugin/src/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type { Options } from '@plugin-web-update-notification/core'
66
import {
77
DIRECTORY_NAME,
88
INJECT_SCRIPT_FILE_NAME,
9+
INJECT_SCRIPT_TAG_ID,
910
INJECT_STYLE_FILE_NAME,
1011
JSON_FILE_NAME,
1112
NOTIFICATION_ANCHOR_CLASS_NAME,
@@ -41,16 +42,14 @@ function injectPluginHtml(
4142
) {
4243
const { customNotificationHTML, hiddenDefaultNotification, injectFileBase = '' } = options
4344

44-
const versionScript = `<script>window.pluginWebUpdateNotice_version = '${version}';</script>`
4545
const cssLinkHtml = customNotificationHTML || hiddenDefaultNotification ? '' : `<link rel="stylesheet" href="${injectFileBase}${DIRECTORY_NAME}/${INJECT_STYLE_FILE_NAME}.${cssFileHash}.css">`
4646
let res = html
4747

4848
res = res.replace(
4949
'<head>',
5050
`<head>
5151
${cssLinkHtml}
52-
<script src="${injectFileBase}${DIRECTORY_NAME}/${INJECT_SCRIPT_FILE_NAME}.${jsFileHash}.js"></script>
53-
${versionScript}`,
52+
<script data-id="${INJECT_SCRIPT_TAG_ID}" data-v="${version}" src="${injectFileBase}${DIRECTORY_NAME}/${INJECT_SCRIPT_FILE_NAME}.${jsFileHash}.js"></script>`,
5453
)
5554

5655
if (!hiddenDefaultNotification) {

packages/webpack-plugin/src/index.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { Options } from '@plugin-web-update-notification/core'
55
import {
66
DIRECTORY_NAME,
77
INJECT_SCRIPT_FILE_NAME,
8+
INJECT_SCRIPT_TAG_ID,
89
INJECT_STYLE_FILE_NAME,
910
JSON_FILE_NAME,
1011
NOTIFICATION_ANCHOR_CLASS_NAME,
@@ -39,17 +40,14 @@ function injectPluginHtml(
3940
) {
4041
const { customNotificationHTML, hiddenDefaultNotification, injectFileBase = '/' } = options
4142

42-
const versionScript = `<script>window.pluginWebUpdateNotice_version = '${version}';</script>`
4343
const cssLinkHtml = customNotificationHTML || hiddenDefaultNotification ? '' : `<link rel="stylesheet" href="${injectFileBase}${DIRECTORY_NAME}/${INJECT_STYLE_FILE_NAME}.${cssFileHash}.css">`
4444
let res = html
4545

4646
res = res.replace(
4747
'<head>',
4848
`<head>
4949
${cssLinkHtml}
50-
<script src="${injectFileBase}${DIRECTORY_NAME}/${INJECT_SCRIPT_FILE_NAME}.${jsFileHash}.js"></script>
51-
52-
${versionScript}`,
50+
<script data-id="${INJECT_SCRIPT_TAG_ID}" data-v="${version}" src="${injectFileBase}${DIRECTORY_NAME}/${INJECT_SCRIPT_FILE_NAME}.${jsFileHash}.js"></script>`,
5351
)
5452

5553
if (!hiddenDefaultNotification) {

0 commit comments

Comments
 (0)