fix: use Vite virtual module for configuration instead of global state
The initial config-store approach failed because Astro's injected routes run in isolated contexts during prerendering and don't have access to global state set during astro:config:setup. Solution: Created a Vite plugin that provides the configuration through a virtual module (virtual:@astrojs/discovery/config) which routes can import at build time. Changes: - Added Vite plugin in astro:config:setup hook - Updated all route handlers to import from virtual module - Changed version from date-based (2025.11.03) to semantic (1.0.0) per npm requirements - Added @ts-ignore comments for virtual module imports Testing: Verified in test project that all configuration now properly passes through to generated files (robots.txt, llms.txt, humans.txt).
This commit is contained in:
parent
d25dde4627
commit
c7b47bba5c
8 changed files with 594 additions and 8 deletions
19
src/index.ts
19
src/index.ts
|
|
@ -59,7 +59,24 @@ export default function discovery(
|
|||
updateConfig({
|
||||
integrations: [
|
||||
sitemap(config.sitemap || {})
|
||||
]
|
||||
],
|
||||
vite: {
|
||||
plugins: [{
|
||||
name: '@astrojs/discovery:config',
|
||||
resolveId(id) {
|
||||
if (id === 'virtual:@astrojs/discovery/config') {
|
||||
return '\0' + id;
|
||||
}
|
||||
return null;
|
||||
},
|
||||
load(id) {
|
||||
if (id === '\0virtual:@astrojs/discovery/config') {
|
||||
return `export default ${JSON.stringify(config, null, 2)};`;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}]
|
||||
}
|
||||
});
|
||||
|
||||
// Inject dynamic routes for discovery files
|
||||
|
|
|
|||
|
|
@ -1,12 +1,12 @@
|
|||
import type { APIRoute } from 'astro';
|
||||
import { generateHumansTxt } from '../generators/humans.js';
|
||||
import { getConfig } from '../config-store.js';
|
||||
// @ts-ignore - Virtual module
|
||||
import config from 'virtual:@astrojs/discovery/config';
|
||||
|
||||
/**
|
||||
* API route for /humans.txt
|
||||
*/
|
||||
export const GET: APIRoute = ({ site }) => {
|
||||
const config = getConfig();
|
||||
const humansConfig = config.humans || {};
|
||||
const siteURL = site || new URL('http://localhost:4321');
|
||||
|
||||
|
|
|
|||
|
|
@ -1,12 +1,12 @@
|
|||
import type { APIRoute } from 'astro';
|
||||
import { generateLLMsTxt } from '../generators/llms.js';
|
||||
import { getConfig } from '../config-store.js';
|
||||
// @ts-ignore - Virtual module
|
||||
import config from 'virtual:@astrojs/discovery/config';
|
||||
|
||||
/**
|
||||
* API route for /llms.txt
|
||||
*/
|
||||
export const GET: APIRoute = async ({ site }) => {
|
||||
const config = getConfig();
|
||||
const llmsConfig = config.llms || {};
|
||||
const siteURL = site || new URL('http://localhost:4321');
|
||||
|
||||
|
|
|
|||
|
|
@ -1,12 +1,12 @@
|
|||
import type { APIRoute } from 'astro';
|
||||
import { generateRobotsTxt } from '../generators/robots.js';
|
||||
import { getConfig } from '../config-store.js';
|
||||
// @ts-ignore - Virtual module
|
||||
import config from 'virtual:@astrojs/discovery/config';
|
||||
|
||||
/**
|
||||
* API route for /robots.txt
|
||||
*/
|
||||
export const GET: APIRoute = ({ site }) => {
|
||||
const config = getConfig();
|
||||
const robotsConfig = config.robots || {};
|
||||
const siteURL = site || new URL('http://localhost:4321');
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue