Skip to content

Commit

Permalink
use promise for config resolving
Browse files Browse the repository at this point in the history
  • Loading branch information
nadar committed Jun 30, 2024
1 parent 6a8a26b commit ccc5a2a
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 23 deletions.
18 changes: 9 additions & 9 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ jobs:
eslint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '20'
- name: Install Dependencies
run: npm ci --legacy-peer-deps
- name: Run ESLint
run: npx eslint
- uses: actions/checkout@v3
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: "20"
- name: Install Dependencies
run: npm ci --legacy-peer-deps
- name: Run ESLint
run: npx eslint
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ import MetaInfoPage from "@flyo/nitro-astro/MetaInfoPage.astro";
const { slug } = Astro.params;
const resolveSlug = slug === undefined ? "" : slug;
const config = useConfig(Astro);
const config = await useConfig(Astro);
let page;
try {
Expand Down Expand Up @@ -88,7 +88,7 @@ To receive the config in the layout in `src/layouts/Layout.astro`:
---
import { useConfig } from "@flyo/nitro-astro";
const config = useConfig(Astro);
const config = await useConfig(Astro);
const { title } = Astro.props;
const currentPath = Astro.url.pathname;
---
Expand Down Expand Up @@ -207,5 +207,6 @@ const isProd = import.meta.env.PROD;
2. `npm run dev`
3. `npm run playground`
4. localhost:4321
5. `npx prettier . --write`

> `npm cache clean --force` fixed an issue where the astro project did not start due to missing dependencies.
9 changes: 4 additions & 5 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ import globals from "globals";
import pluginJs from "@eslint/js";
import tseslint from "typescript-eslint";


export default [
{
files: ["**/*.{js,mjs,cjs,ts}"]
files: ["**/*.{js,mjs,cjs,ts}"],
},
{
ignores: ["lib/dist/", "playground/"]
ignores: ["lib/dist/", "playground/"],
},
{languageOptions: { globals: globals.node }},
{ languageOptions: { globals: globals.node } },
pluginJs.configs.recommended,
...tseslint.configs.recommended,
];
];
4 changes: 2 additions & 2 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ export function useConfigApi(): ConfigApi {
return new ConfigApi(useConfiguration());
}

export function useConfig(astro: AstroGlobal): ConfigResponse {
return astro.locals.config;
export async function useConfig(astro: AstroGlobal): Promise<ConfigResponse> {
return await astro.locals.config;
}

export function useEntitiesApi(): EntitiesApi {
Expand Down
18 changes: 16 additions & 2 deletions lib/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
import { defineMiddleware } from "astro:middleware";
import { useConfigApi } from "./index.ts";

export const onRequest = defineMiddleware(async (context, next) => {
context.locals.config = await useConfigApi().config({
let resolvedValue;

async function getConfigPromise(context) {
console.log("request config request");
if (resolvedValue) {
// If the value is already resolved, return a resolved promise with the value
return resolvedValue;
}
console.log("config request");
// Fetch the config and store the resolved value
const value = await useConfigApi().config({
lang: context.currentLocale,
});
resolvedValue = value;
return value;
}

export const onRequest = defineMiddleware((context, next) => {
context.locals.config = getConfigPromise(context);
return next();
});
1 change: 0 additions & 1 deletion lib/sitemap.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { useSitemapApi } from "./index.ts";
import type { AstroGlobal } from "astro";


function buildUrl(path: string, domain: string) {
return `${domain.replace(/\/$/, "")}/${path.replace(/^\//, "")}`;
}
Expand Down
2 changes: 1 addition & 1 deletion playground/src/layouts/Layout.astro
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
import { useConfig } from "@flyo/nitro-astro";
const config = useConfig(Astro);
const config = await useConfig(Astro);
const { title } = Astro.props;
const currentPath = Astro.url.pathname;
const currentLocal = Astro.currentLocale;
Expand Down
2 changes: 1 addition & 1 deletion playground/src/pages/[...slug].astro
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import MetaInfoPage from "@flyo/nitro-astro/MetaInfoPage.astro";
const { slug } = Astro.params;
const resolveSlug = slug === undefined ? "" : slug;
const config = useConfig(Astro);
const config = await useConfig(Astro);
let page;
try {
Expand Down

0 comments on commit ccc5a2a

Please sign in to comment.