New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

astro-als

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

astro-als

Avoid hydration mismatches by getting the data on both server and client using an AsyncLocalStorage in Astro.

  • 0.0.4
  • latest
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

[!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:

  1. Install the required dependencies
pnpm add astro-als
npm install astro-als
yarn add astro-als
  1. 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:

// als.config.ts
import { defineAlsConfig } from "astro-als/config";

export default defineAlsConfig({
	seedData(context) {
		return {
			// Your serializable data here
		};
	},
});

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 package
  • package 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.

Keywords

FAQs

Package last updated on 10 Sep 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc