Skip to content

Commit

Permalink
fix astro reloading issue when in single repo mode (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
theoephraim authored May 23, 2024
1 parent f16be40 commit c63c2e9
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 22 deletions.
6 changes: 6 additions & 0 deletions .changeset/lazy-peas-flash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@dmno/astro-integration": patch
"dmno": patch
---

astro reload issue in single repo mode
7 changes: 4 additions & 3 deletions packages/core/src/app-init/inject-dmno-globals.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import type { InjectedDmnoEnv, InjectedDmnoEnvItem } from '../config-engine/config-engine';

const originalProcessEnv = structuredClone(process.env);
const sensitiveValueLookup: Record<string, { value: string, masked: string }> = {};
const publicDynamicKeys: Array<string> = [];
const sensitiveKeys: Array<string> = [];

export function injectDmnoGlobals(
opts?: {
Expand All @@ -13,6 +10,10 @@ export function injectDmnoGlobals(
onItemAccess?: (item: InjectedDmnoEnvItem) => void;
},
) {
const sensitiveValueLookup: Record<string, { value: string, masked: string }> = {};
const publicDynamicKeys: Array<string> = [];
const sensitiveKeys: Array<string> = [];

// if we've already injected the globals and we didnt have any options passed in, we can bail
if (!opts && (globalThis as any).DMNO_CONFIG) {
return {};
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/config-engine/type-generation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export async function generateTypescriptTypes(service: DmnoService) {
tsSrc.push('\n');

const publicKeysForPick = _.map(publicKeys, JSON.stringify).join(' | ');
tsSrc.push(`export type DmnoGeneratedPublicConfigSchema = Pick<DmnoGeneratedConfigSchema, ${publicKeysForPick}>`);
tsSrc.push(`export type DmnoGeneratedPublicConfigSchema = Pick<DmnoGeneratedConfigSchema, ${publicKeysForPick || 'never'}>`);

return tsSrc.join('\n');
}
Expand Down
61 changes: 43 additions & 18 deletions packages/integrations/astro/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,53 @@ import type { AstroIntegration } from 'astro';
// console.log('dmno astro integration file loaded!');


/// initialize a dmno config server client, but only once
// initialize a dmno config server client, but only once
(process as any).dmnoConfigClient ||= new ConfigServerClient();
const dmnoConfigClient: ConfigServerClient = (process as any).dmnoConfigClient;

const dmnoService = await dmnoConfigClient.getServiceConfig();

const configItemKeysAccessed: Record<string, boolean> = {};
const dmnoConfigValid = ConfigServerClient.checkServiceIsValid(dmnoService);
// Unsure why, but in some cases this whole file reloads, and in others it doesnt
// so we may need to reload the config multiple times
// TODO: try to clean this logic up a bit?
let dmnoService: Awaited<ReturnType<typeof dmnoConfigClient.getServiceConfig>>;
let configItemKeysAccessed: Record<string, boolean> = {};
let dmnoConfigValid = true;
let publicDynamicItemKeys: Array<string> = [];
let sensitiveItemKeys: Array<string> = [];
let sensitiveValueLookup: Record<string, string> = {};

async function reloadDmnoConfig() {
dmnoService = await dmnoConfigClient.getServiceConfig();
dmnoConfigValid = ConfigServerClient.checkServiceIsValid(dmnoService);
configItemKeysAccessed = {};

injectDmnoGlobals({
injectedConfig: serializedServiceToInjectedConfig(dmnoService),
trackingObject: configItemKeysAccessed,
dynamicAccessDisabled: true,
});
publicDynamicItemKeys = (globalThis as any)._DMNO_PUBLIC_DYNAMIC_KEYS;
sensitiveItemKeys = (globalThis as any)._DMNO_SENSITIVE_KEYS;
sensitiveValueLookup = {};
for (const itemKey of sensitiveItemKeys) {
const val = (globalThis as any).DMNO_CONFIG[itemKey];
if (val) sensitiveValueLookup[itemKey] = val.toString();
}
}

// we do want to run this right away so the globals get injected into the astro.config file
await reloadDmnoConfig();

injectDmnoGlobals({
injectedConfig: serializedServiceToInjectedConfig(dmnoService),
trackingObject: configItemKeysAccessed,
dynamicAccessDisabled: true,
});

let enableDynamicPublicClientLoading = false;
let astroCommand: 'dev' | 'build' | 'preview' = 'dev';

const publicDynamicConfigItemKeys = (globalThis as any)._DMNO_PUBLIC_DYNAMIC_KEYS;

const sensitiveItemKeys = (globalThis as any)._DMNO_SENSITIVE_KEYS as Array<string>;
const sensitiveValueLookup: Record<string, string> = {};
for (const itemKey of sensitiveItemKeys) {
const val = (globalThis as any).DMNO_CONFIG[itemKey];
if (val) sensitiveValueLookup[itemKey] = val.toString();
}



let dmnoHasTriggeredReload = false;

type DmnoAstroIntegrationOptions = {
// TODO: figure out options - loading dynamic public config?
};
Expand All @@ -43,6 +62,11 @@ function dmnoAstroIntegration(dmnoIntegrationOpts?: DmnoAstroIntegrationOptions)
name: 'dmno-astro-integration',
hooks: {
'astro:config:setup': async (opts) => {
// this handles the case where astro's vite server reloaded but this file did not get reloaded
if (dmnoHasTriggeredReload) {
await reloadDmnoConfig();
dmnoHasTriggeredReload = false;
}
const {
isRestart, logger, addDevToolbarApp, updateConfig,
injectScript, addMiddleware, injectRoute,
Expand All @@ -57,7 +81,7 @@ function dmnoAstroIntegration(dmnoIntegrationOpts?: DmnoAstroIntegrationOptions)
if (opts.config.output === 'static') {
enableDynamicPublicClientLoading = false;
} else {
enableDynamicPublicClientLoading = publicDynamicConfigItemKeys.length > 0;
enableDynamicPublicClientLoading = publicDynamicItemKeys.length > 0;
}

const staticConfigReplacements = {} as Record<string, string>;
Expand Down Expand Up @@ -102,6 +126,7 @@ function dmnoAstroIntegration(dmnoIntegrationOpts?: DmnoAstroIntegrationOptions)
dmnoConfigClient.eventBus.on('reload', () => {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
server.restart();
dmnoHasTriggeredReload = true;
});
}
},
Expand Down Expand Up @@ -163,7 +188,7 @@ function dmnoAstroIntegration(dmnoIntegrationOpts?: DmnoAstroIntegrationOptions)
return window._DMNO_PUBLIC_DYNAMIC_CONFIG[key];
}
` : `
if (${JSON.stringify(publicDynamicConfigItemKeys)}.includes(key)) {
if (${JSON.stringify(publicDynamicItemKeys)}.includes(key)) {
throw new Error(\`❌ Unable to access dynamic config item \\\`\${key}\\\` in Astro "static" output mode\`);
}
`,
Expand Down

0 comments on commit c63c2e9

Please # to comment.