Skip to content

Instantly share code, notes, and snippets.

@imeredith
Last active October 23, 2024 22:56
Show Gist options
  • Save imeredith/683bf1089607e85a00ee26403420edf4 to your computer and use it in GitHub Desktop.
Save imeredith/683bf1089607e85a00ee26403420edf4 to your computer and use it in GitHub Desktop.
Conditional Routes
import type { AstroIntegration } from "astro";
import { glob } from "glob";
import path from "path";
const createPlugin = (redirects: Record<string, string>): AstroIntegration => {
return {
name: "astro-plugin-restricted-pages",
hooks: {
"astro:config:setup": async ({ injectRoute }) => {
const baseDir = "src/pages-restricted";
const restrictedPages = await glob(`**/*.astro`, {
cwd: path.join(process.cwd(), baseDir),
});
for (const page of restrictedPages) {
const route = `/${page.replace(/\.astro$/, "")}`;
injectRoute({
pattern: route,
entrypoint: `./${baseDir}/${page}`,
});
}
},
},
};
};
export default createPlugin;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment