Last active
October 23, 2024 22:56
-
-
Save imeredith/683bf1089607e85a00ee26403420edf4 to your computer and use it in GitHub Desktop.
Conditional Routes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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