Skip to content

Commit

Permalink
fix: (vite) prepend base on post path for devtools
Browse files Browse the repository at this point in the history
When serving an app with vite under a subdirectory, the `POST_PATH` for devtools also has to include the subdirectory when checking the request urls.
  • Loading branch information
flipace committed Jul 6, 2023
1 parent 36ae01f commit e333094
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions packages/vite/src/devtool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const MODULES_MAP: Record<string, string | undefined> = {
[MOCK_CLASSES_MODULE_ID]: MOCK_CLASSES_PATH,
}

const POST_PATH = '/@unocss-devtools-update'
const BASE_POST_PATH = '/@unocss-devtools-update'

function getBodyJson(req: IncomingMessage) {
return new Promise<any>((resolve, reject) => {
Expand All @@ -45,6 +45,7 @@ export function createDevtoolsPlugin(ctx: UnocssPluginContext): Plugin[] {
let clientCode = ''
let devtoolTimer: any
let lastUpdate = Date.now()
let postPath = BASE_POST_PATH

function toClass(name: string) {
// css escape
Expand Down Expand Up @@ -91,13 +92,14 @@ export function createDevtoolsPlugin(ctx: UnocssPluginContext): Plugin[] {

configResolved(_config) {
config = _config
postPath = `${(config.base?.replace(/\/$/, '') ?? '')}${BASE_POST_PATH}`
},

configureServer(_server) {
server = _server

server.middlewares.use(async (req, res, next) => {
if (req.url !== POST_PATH)
if (req.url !== postPath)
return next()

try {
Expand Down Expand Up @@ -141,7 +143,7 @@ export function createDevtoolsPlugin(ctx: UnocssPluginContext): Plugin[] {
`import('${DEVTOOLS_CSS_PATH}')`,
]
.join('\n')
.replace('__POST_PATH__', (config.server?.origin ?? '') + POST_PATH)
.replace('__POST_PATH__', `${(config.server?.origin ?? '')}${postPath}`)
}
return config.command === 'build'
? ''
Expand Down

0 comments on commit e333094

Please sign in to comment.