Skip to content

Commit

Permalink
fix: 修复publicPath
Browse files Browse the repository at this point in the history
  • Loading branch information
senwii committed Dec 31, 2019
1 parent a912fc9 commit c2f7bab
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 16 deletions.
6 changes: 3 additions & 3 deletions public/404.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@
// https://username.github.io/repo-name/one/two?a=b&c=d#qwe becomes
// https://username.github.io/repo-name/?p=/one/two&q=a=b~and~c=d#qwe
// Otherwise, leave segmentCount as 0.
var segmentCount = 0;
var segmentCount = 0

var l = window.location;
var l = window.location
l.replace(
l.protocol + '//' + l.hostname + (l.port ? ':' + l.port : '') +
l.pathname.split('/').slice(0, 1 + segmentCount).join('/') + '/?p=/' +
l.pathname.slice(1).split('/').slice(segmentCount).join('/').replace(/&/g, '~and~') +
(l.search ? '&q=' + l.search.slice(1).replace(/&/g, '~and~') : '') +
l.hash
);
)

</script>
</head>
Expand Down
27 changes: 15 additions & 12 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,21 @@
// the single page app to route accordingly.
(function(l) {
if (l.search) {
var q = {};
l.search.slice(1).split('&').forEach(function(v) {
var a = v.split('=');
q[a[0]] = a.slice(1).join('=').replace(/~and~/g, '&');
});
if (q.p !== undefined) {
window.history.replaceState(null, null,
l.pathname.slice(0, -1) + (q.p || '') +
(q.q ? ('?' + q.q) : '') +
l.hash
);
}
var basename = '<%= process.env.PROJECT_NAME %>'
var q = {}
l.search
.slice(1)
.split('&')
.forEach(function(v) {
var a = v.split('=')
q[a[0]] = a.slice(1).join('=').replace(/~and~/g, '&')
})
if (q.p !== undefined) {
window.history.replaceState(
null,
null,
l.pathname.replace(`^\/${basename}\/`, '/').slice(0, -1) + (q.p || '') + (q.q ? ('?' + q.q) : '') + l.hash)
}
}
}(window.location))
</script>
Expand Down
2 changes: 1 addition & 1 deletion src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const routes = [
},
]

const basename = '/react-template-project/'
const basename = `/${process.env.PROJECT_NAME}/`

export {
basename,
Expand Down
5 changes: 5 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const webpack = require('webpack')
const appDirName = process.cwd()
const packageName = require(`${appDirName}/package.json`).name
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const CopyWebpackPlugin = require('copy-webpack-plugin')
Expand Down Expand Up @@ -31,6 +33,9 @@ const plugins = [
flatten: true,
},
]),
new webpack.DefinePlugin({
'process.env.PROJECT_NAME': JSON.stringify(packageName),
}),
]

if (IS_PRODUCTION) {
Expand Down

0 comments on commit c2f7bab

Please sign in to comment.