From 76653d991f5eaf58df0bae5165e03618ad6f5c17 Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Tue, 16 May 2023 16:51:37 -0500 Subject: [PATCH] initial commit --- examples/blog/astro.config.mjs | 3 +++ packages/astro/package.json | 1 + packages/astro/src/core/config/schema.ts | 5 +++++ packages/astro/src/integrations/index.ts | 4 ++++ packages/astro/src/runtime/client/router.ts | 10 ++++++++++ 5 files changed, 23 insertions(+) create mode 100644 packages/astro/src/runtime/client/router.ts diff --git a/examples/blog/astro.config.mjs b/examples/blog/astro.config.mjs index 3b2f75c84..644241cf2 100644 --- a/examples/blog/astro.config.mjs +++ b/examples/blog/astro.config.mjs @@ -7,4 +7,7 @@ import sitemap from '@astrojs/sitemap'; export default defineConfig({ site: 'https://example.com', integrations: [mdx(), sitemap()], + experimental: { + router: 'spa' + } }); diff --git a/packages/astro/package.json b/packages/astro/package.json index 7acfa77e6..bef365bb1 100644 --- a/packages/astro/package.json +++ b/packages/astro/package.json @@ -146,6 +146,7 @@ "js-yaml": "^4.1.0", "kleur": "^4.1.4", "magic-string": "^0.27.0", + "micromorph": "^0.4.4", "mime": "^3.0.0", "ora": "^6.1.0", "path-to-regexp": "^6.2.1", diff --git a/packages/astro/src/core/config/schema.ts b/packages/astro/src/core/config/schema.ts index 1c246a4a8..c25b61796 100644 --- a/packages/astro/src/core/config/schema.ts +++ b/packages/astro/src/core/config/schema.ts @@ -43,6 +43,7 @@ const ASTRO_CONFIG_DEFAULTS: AstroUserConfig & any = { customClientDirectives: false, inlineStylesheets: 'never', middleware: false, + router: 'mpa' }, }; @@ -209,6 +210,10 @@ export const AstroConfigSchema = z.object({ .default(ASTRO_CONFIG_DEFAULTS.experimental.inlineStylesheets), middleware: z.oboolean().optional().default(ASTRO_CONFIG_DEFAULTS.experimental.middleware), hybridOutput: z.boolean().optional().default(ASTRO_CONFIG_DEFAULTS.experimental.hybridOutput), + router: z + .enum(['mpa', 'spa']) + .optional() + .default(ASTRO_CONFIG_DEFAULTS.experimental.router), }) .passthrough() .refine( diff --git a/packages/astro/src/integrations/index.ts b/packages/astro/src/integrations/index.ts index 5d88a1196..a1e794d2b 100644 --- a/packages/astro/src/integrations/index.ts +++ b/packages/astro/src/integrations/index.ts @@ -59,6 +59,10 @@ export async function runHookConfigSetup({ let updatedConfig: AstroConfig = { ...settings.config }; let updatedSettings: AstroSettings = { ...settings, config: updatedConfig }; let addedClientDirectives = new Map>(); + + if (settings.config.experimental.router === 'spa') { + updatedSettings.scripts.push({ stage: 'page', content: 'import "astro/client/router.js"' }); + } for (const integration of settings.config.integrations) { /** diff --git a/packages/astro/src/runtime/client/router.ts b/packages/astro/src/runtime/client/router.ts new file mode 100644 index 000000000..846552986 --- /dev/null +++ b/packages/astro/src/runtime/client/router.ts @@ -0,0 +1,10 @@ +import listen from 'micromorph/spa'; + +listen({ + beforeDiff(doc) { + console.log('before diff') + }, + afterDiff() { + console.log('after diff') + } +})