Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hot key to reload content layer #11626

Merged
merged 6 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ export class ContentLayer {
ctx.status === 'loaded' &&
ctx.config.digest !== this.#lastConfigDigest
) {
this.#lastConfigDigest = ctx.config.digest;
this.sync();
}
});
Expand Down Expand Up @@ -128,9 +127,10 @@ export class ContentLayer {
logger.debug('Content config not loaded, skipping sync');
return;
}
const { digest: currentConfigDigest } = contentConfig.config;
this.#lastConfigDigest = currentConfigDigest;

const previousConfigDigest = await this.#store.metaStore().get('config-digest');
const { digest: currentConfigDigest } = contentConfig.config;
if (currentConfigDigest && previousConfigDigest !== currentConfigDigest) {
logger.info('Content config changed, clearing cache');
this.#store.clearAll();
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/core/dev/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import { gt, major, minor, patch } from 'semver';
import type * as vite from 'vite';
import type { AstroInlineConfig } from '../../@types/astro.js';
import { DATA_STORE_FILE } from '../../content/consts.js';
import { globalContentLayer } from '../../content/content-layer.js';
import { DataStore, globalDataStore } from '../../content/data-store.js';
import { attachContentServerListeners } from '../../content/index.js';
import { globalContentLayer } from '../../content/sync.js';
import { globalContentConfigObserver } from '../../content/utils.js';
import { telemetry } from '../../events/index.js';
import * as msg from '../messages.js';
Expand Down
15 changes: 12 additions & 3 deletions packages/astro/src/core/dev/restart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type nodeFs from 'node:fs';
import { fileURLToPath } from 'node:url';
import * as vite from 'vite';
import type { AstroInlineConfig, AstroSettings } from '../../@types/astro.js';
import { globalContentLayer } from '../../content/content-layer.js';
import { eventCliSession, telemetry } from '../../events/index.js';
import { createNodeLogger, createSettings, resolveConfig } from '../config/index.js';
import { collectErrorMetadata } from '../errors/dev/utils.js';
Expand Down Expand Up @@ -169,11 +170,19 @@ export async function createContainerWithAutomaticRestart({
// Ignore the `forceOptimize` parameter for now.
restart.container.viteServer.restart = () => handleServerRestart();

// Set up shortcuts, overriding Vite's default shortcuts so it works for Astro
// Set up shortcuts
restart.container.viteServer.bindCLIShortcuts({
customShortcuts: [
// Disable Vite's builtin "r" (restart server), "u" (print server urls) and "c" (clear console) shortcuts
{ key: 'r', description: '' },
{
key: 'r',
description: 'reload content layer',
action: () => {
if (globalContentLayer.initialized()) {
globalContentLayer.get().sync();
}
},
},
// Disable default Vite shortcuts that don't work well with Astro
{ key: 'u', description: '' },
{ key: 'c', description: '' },
],
Expand Down
2 changes: 2 additions & 0 deletions packages/astro/src/core/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ export function serverStart({
...localUrlMessages,
...networkUrlMessages,
'',
`${dim('Press')} ${bold('h + enter')} ${dim('to')} ${bold('show hot keys')}`,
''
ascorbic marked this conversation as resolved.
Show resolved Hide resolved
];
return messages.filter((msg) => typeof msg === 'string').join('\n');
}
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/core/sync/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { type HMRPayload, createServer } from 'vite';
import type { AstroConfig, AstroInlineConfig, AstroSettings } from '../../@types/astro.js';
import { getPackage } from '../../cli/install-package.js';
import { DATA_STORE_FILE } from '../../content/consts.js';
import { globalContentLayer } from '../../content/content-layer.js';
import { DataStore, globalDataStore } from '../../content/data-store.js';
import { createContentTypesGenerator } from '../../content/index.js';
import { globalContentLayer } from '../../content/sync.js';
import { globalContentConfigObserver } from '../../content/utils.js';
import { syncAstroEnv } from '../../env/sync.js';
import { telemetry } from '../../events/index.js';
Expand Down
Loading