Skip to content

Commit fb8588e

Browse files
cheeeduardoboucas
andauthored
fix: apply fixes required for Vite plugin in Astro (#332)
* fix: Use same directory for tmpdir to prevent cross-volume issues * fix: create and destroy netlify dev server bundle in the same place gets rid of the plugin-level netlifyDev let, and we no longer need to check if the middleware doesn't exist * Remove leading 0. from random file name Co-authored-by: Eduardo Bouças <[email protected]> * make tmp string more aestheic --------- Co-authored-by: Eduardo Bouças <[email protected]>
1 parent 99f0bcf commit fb8588e

File tree

2 files changed

+13
-23
lines changed

2 files changed

+13
-23
lines changed

packages/blobs/src/server.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { createHmac } from 'node:crypto'
22
import { createReadStream, promises as fs } from 'node:fs'
3-
import { tmpdir } from 'node:os'
43
import { dirname, join, relative, resolve, sep } from 'node:path'
54

65
import { HTTPServer } from '@netlify/dev-utils'
@@ -318,10 +317,10 @@ export class BlobsServer {
318317
// happen if multiple clients try to write to the same key at the same
319318
// time. To prevent this, we write to a temporary file first and then
320319
// atomically move it to its final destination.
321-
const tempPath = join(tmpdir(), Math.random().toString())
320+
const tempPath = join(dirname(dataPath), `.${Math.random().toString(36).slice(2)}`)
322321
const body = await req.arrayBuffer()
323-
await fs.writeFile(tempPath, Buffer.from(body))
324322
await fs.mkdir(dirname(dataPath), { recursive: true })
323+
await fs.writeFile(tempPath, Buffer.from(body))
325324
await fs.rename(tempPath, dataPath)
326325

327326
if (metadata) {

packages/vite-plugin/src/main.ts

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,18 @@ export default function netlify(options: NetlifyPluginOptions = {}): any {
2222
return []
2323
}
2424

25-
let netlifyDev: NetlifyDev | undefined
26-
2725
const plugin: vite.Plugin = {
2826
name: 'vite-plugin-netlify',
2927
async configureServer(viteDevServer) {
28+
// if the vite dev server's http server isn't ready (or we're in
29+
// middleware mode) let's not get involved
30+
if (!viteDevServer.httpServer) {
31+
return
32+
}
3033
const logger = createLoggerFromViteLogger(viteDevServer.config.logger)
3134
const { blobs, edgeFunctions, functions, middleware = true, redirects, staticFiles } = options
3235

33-
netlifyDev = new NetlifyDev({
36+
const netlifyDev = new NetlifyDev({
3437
blobs,
3538
edgeFunctions,
3639
functions,
@@ -45,21 +48,15 @@ export default function netlify(options: NetlifyPluginOptions = {}): any {
4548
})
4649

4750
await netlifyDev.start()
51+
52+
viteDevServer.httpServer.once('close', () => {
53+
netlifyDev.stop()
54+
})
55+
4856
logger.log('Environment loaded')
4957

5058
if (middleware) {
5159
viteDevServer.middlewares.use(async function netlifyPreMiddleware(nodeReq, nodeRes, next) {
52-
// This should never happen, but adding this escape hatch just in case.
53-
if (!netlifyDev) {
54-
logger.error(
55-
'Some primitives will not work as expected due to an unknown error. Please restart your application.',
56-
)
57-
58-
next()
59-
60-
return
61-
}
62-
6360
const headers: Record<string, string> = {}
6461
const result = await netlifyDev.handleAndIntrospectNodeRequest(nodeReq, {
6562
headersCollector: (key, value) => {
@@ -93,12 +90,6 @@ export default function netlify(options: NetlifyPluginOptions = {}): any {
9390
)
9491
}
9592
},
96-
97-
async closeBundle() {
98-
await netlifyDev?.stop()
99-
100-
netlifyDev = undefined
101-
},
10293
}
10394

10495
return [plugin]

0 commit comments

Comments
 (0)