mirror of
https://github.com/withastro/astro.git
synced 2025-04-07 23:41:43 -05:00
fix: invalidate data store module when dev server starts (#12938)
* fix: invalidate data store module when dev server starts * Check for dev
This commit is contained in:
parent
d48683698e
commit
dbb04f3c04
2 changed files with 24 additions and 16 deletions
5
.changeset/strong-scissors-sip.md
Normal file
5
.changeset/strong-scissors-sip.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'astro': patch
|
||||
---
|
||||
|
||||
Fixes a bug where content collections would sometimes appear empty when first running astro dev
|
|
@ -46,6 +46,17 @@ interface AstroContentVirtualModPluginParams {
|
|||
fs: typeof nodeFs;
|
||||
}
|
||||
|
||||
function invalidateDataStore(server: ViteDevServer) {
|
||||
const module = server.moduleGraph.getModuleById(RESOLVED_DATA_STORE_VIRTUAL_ID);
|
||||
if (module) {
|
||||
server.moduleGraph.invalidateModule(module);
|
||||
}
|
||||
server.ws.send({
|
||||
type: 'full-reload',
|
||||
path: '*',
|
||||
});
|
||||
}
|
||||
|
||||
export function astroContentVirtualModPlugin({
|
||||
settings,
|
||||
fs,
|
||||
|
@ -59,8 +70,12 @@ export function astroContentVirtualModPlugin({
|
|||
dataStoreFile = getDataStoreFile(settings, env.command === 'serve');
|
||||
},
|
||||
buildStart() {
|
||||
// We defer adding the data store file to the watcher until the server is ready
|
||||
devServer?.watcher.add(fileURLToPath(dataStoreFile));
|
||||
if (devServer) {
|
||||
// We defer adding the data store file to the watcher until the server is ready
|
||||
devServer.watcher.add(fileURLToPath(dataStoreFile));
|
||||
// Manually invalidate the data store to avoid a race condition in file watching
|
||||
invalidateDataStore(devServer);
|
||||
}
|
||||
},
|
||||
async resolveId(id) {
|
||||
if (id === VIRTUAL_MODULE_ID) {
|
||||
|
@ -164,29 +179,17 @@ export function astroContentVirtualModPlugin({
|
|||
configureServer(server) {
|
||||
devServer = server;
|
||||
const dataStorePath = fileURLToPath(dataStoreFile);
|
||||
|
||||
function invalidateDataStore() {
|
||||
const module = server.moduleGraph.getModuleById(RESOLVED_DATA_STORE_VIRTUAL_ID);
|
||||
if (module) {
|
||||
server.moduleGraph.invalidateModule(module);
|
||||
}
|
||||
server.ws.send({
|
||||
type: 'full-reload',
|
||||
path: '*',
|
||||
});
|
||||
}
|
||||
|
||||
// If the datastore file changes, invalidate the virtual module
|
||||
|
||||
server.watcher.on('add', (addedPath) => {
|
||||
if (addedPath === dataStorePath) {
|
||||
invalidateDataStore();
|
||||
invalidateDataStore(server);
|
||||
}
|
||||
});
|
||||
|
||||
server.watcher.on('change', (changedPath) => {
|
||||
if (changedPath === dataStorePath) {
|
||||
invalidateDataStore();
|
||||
invalidateDataStore(server);
|
||||
}
|
||||
});
|
||||
},
|
||||
|
|
Loading…
Add table
Reference in a new issue