Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: Astro.context #230

Closed
wants to merge 1 commit into from
Closed

Conversation

laptou
Copy link

@laptou laptou commented Jul 18, 2022

  • Start Date: 2022-07-17
  • Status: Active

Summary

Adds a "context" available in Astro files during SSR that can be provided from the SSR entrypoint. In template files, this is Astro.context, and in API routes, this is the context property of the route's parameter. The purpose of this change is to make
it possible for state that is already derived by the server (such as
authentication state) to easily be passed from Express/Koa to an Astro template
via Astro.context, so that this information does not have to be re-derived
from Astro.request.headers.

A minimal example

In the client, in an Astro template file:

<html lang="en">
	<head>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width" />
		<title>Astro</title>
	</head>
	<body>
		<p>Welcome, {Astro.context.user.name}</p>
	</body>
</html>

In the client, in an API endpoint:

export function get({ params, context }) {
  return context.user.name;
}

In the server, using NodeJS SSR adapter:

import { createServer } from 'http';
const { handler } = await import('./path-to-entrypoint.mjs');
server = createServer((req, res) => {
  handler(req, res, { user: { name: 'Elvis' } });
});
server.listen();

Links

@laptou laptou changed the title Add RFC for Astro.context RFC: Astro.context Jul 18, 2022

# Drawbacks

This change, as described, would only be usable via the NodeJS adapter. Further
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think this is a bit of a problem. I think we would be hesitant to add a property to the Astro global that only worked in one adapter. If we had an idea to make it work for others that would make it more palpable.

In the past we have discussed ideas of middleware within Astro itself. Then presumably an Astro middleware could mutate a context object like you've described in this RFC.

@RichiCoder1
Copy link

RichiCoder1 commented Aug 9, 2022

As mentioned here: #221 (reply in thread)

A generic mechanism where user code can expose a method that takes in adapter-specific details and outputs a "generic" context would be ideal. Something like:

export interface RequestContext {
	user: IUser;
}

export function beforeRequest(event: RequestEvent): Promise<RequestContext> {
	// do auth stuff
	const user = await /* ... */;
	return {
		user {
			name: user.name,
			id: user.id
		}
	};
}

Though this specific and common case could also be handled by something like svelete's specifically designed session helper: https://kit.svelte.dev/docs/hooks#getsession

So the above would turn into

export interface MySession {
	user: IUser;
}

export function getSession(event: RequestEvent): Promise<MySession> {
	// do auth stuff
	const user = await /* ... */;
	return {
		user {
			name: user.name,
			id: user.id
		}
	};
}

@matthewp
Copy link
Contributor

@RichiCoder1 How would this work in dev?

I think the underlying problem with this and related ideas is figuring out what role adapters should play in dev. Once we figure that out then features like this will be a bit easier to build.

@matthewp
Copy link
Contributor

matthewp commented Sep 2, 2022

This idea came up again in relation to Cloudflare's env feature. https://discord.com/channels/830184174198718474/1015040723030118522

@gijsroge
Copy link

I kinda asked the same I think. https://discord.com/channels/830184174198718474/1021517008061345892

@pilcrowonpaper
Copy link

Is it possible to implement something similar to SvelteKit's handle() function in hooks? It runs before server requests on SSR and on build step as well. This would make context available in all deployments regardless of the adapter.

@wassfila
Copy link

@RichiCoder1 How would this work in dev?

I think the underlying problem with this and related ideas is figuring out what role adapters should play in dev. Once we figure that out then features like this will be a bit easier to build.

as of Today, middleware is not usable in dev mode, so why make this RFC depend on that ? I also don't see why this would not work with deno.

@natemoo-re
Copy link
Member

natemoo-re commented Mar 23, 2023

I'm going to propose that we close this RFC. Middleware is officially on the roadmap now, and it's likely that we'll be addressing this use case through that API.

Edit: updated the Stage 2 proposal to link back to this discussion

@natemoo-re natemoo-re mentioned this pull request Mar 23, 2023
@natemoo-re natemoo-re closed this Mar 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants