[!WARNING]
This package is deprecated. Use https://inox-tools.fryuni.dev/request-state instead.
astro-als
This is an Astro integration that allows you to avoid hydration mismatches by getting the data on both server and client using an AsyncLocalStorage.
Usage
Prerequisites
When using the Cloudflare adapter, you'll need to enable AsyncLocalStorage manually.
Limitations
- All data must be serializable
- The data will be be made static on prerendered pages
Installation
Install the integration automatically using the Astro CLI:
pnpm astro add astro-als
npx astro add astro-als
yarn astro add astro-als
Or install it manually:
- Install the required dependencies
pnpm add astro-als
npm install astro-als
yarn add astro-als
- Add the integration to your astro config
+import als from "astro-als";
export default defineConfig({
integrations: [
+ als(),
],
});
Create a config file
In your project root, create a new als.config.ts
file:
import { defineAlsConfig } from "astro-als/config";
export default defineAlsConfig({
seedData(context) {
return {
};
},
});
This code will be run in a post middleware. The best practise is to only use the seedData
function to return data without causing any side-effects.
Use the component
In a shared layout, import AlsSerialize.astro
:
---
// src/layouts/Layout.astro
import AlsSerialize from "astro-als/AlsSerialize.astro"
interface Props {
title: string;
}
const { title } = Astro.props;
---
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="description" content="Astro description" />
<meta name="viewport" content="width=device-width" />
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
<meta name="generator" content={Astro.generator} />
<title>{title}</title>
<AlsSerialize />
</head>
<body>
<slot />
</body>
</html>
Use the data
You can now import the data server (within a request) and client side using getAlsData
:
<script setup lang="ts">
// src/components/Test.vue
import { getAlsData } from "als:astro";
const data = getAlsData();
</script>
<template>
<pre>{{ JSON.stringify(data, null, 2) }}</pre>
</template>
---
// src/pages/index.astro
import Layout from "../layouts/Layout.astro";
import Test from "../components/Test.vue";
---
<Layout title="Welcome to Astro.">
<Test client:load />
</Layout>
Integration configuration
Here is the TypeScript type:
export type Options = {
configFile?: string;
cliendId?: string;
disableConfigValidation?: boolean;
}
configFile
Config file path for the integration, relative to the root directory. Defaults to "als.config"
.
import als from "astro-als";
export default defineConfig({
integrations: [
als({
configFile: "./my-custom-config.mjs"
}),
],
});
clientId
Id used in the DOM to store the data. Defaults to "astro-als-data"
.
import als from "astro-als";
export default defineConfig({
integrations: [
als({
clientId: "my-custom-id"
}),
],
});
disableConfigValidation
Disables validation of the ALS Config file, eg. when there are conflicts with other packages. Defaults to false
.
import als from "astro-als";
export default defineConfig({
integrations: [
als({
disableConfigValidation: true
}),
],
});
Contributing
This package is structured as a monorepo:
playground
contains code for testing the packagepackage
contains the actual package
Install dependencies using pnpm:
pnpm i --frozen-lockfile
Start the playground and package watcher:
pnpm dev
You can now edit files in package
. Please note that making changes to those files may require restarting the playground dev server.
Licensing
MIT Licensed. Made with ❤️ by Florian Lefebvre.