withastro / roadmap

Ideas, suggestions, and formal RFC proposals for the Astro project.
273 stars 29 forks source link

Unable to pass variable from the configuration (integration) to (static file) endpoint #650

Closed kireerik closed 11 months ago

kireerik commented 1 year ago

What version of astro are you using?

2.5.5

Are you using an SSR adapter? If so, which one?

no

What package manager are you using?

pnpm

What operating system are you using?

Windows

What browser are you using?

Chrome

Describe the Bug

I have tried it like this: astro.config.js:

import {defineConfig} from 'astro/config'

export default defineConfig({
    integrations: [
        {
            name: ''
            , hooks: {
                'astro:server:setup': ({server: {middlewares}}) => {
                    middlewares.use((request, response, next) => {
                        response.locals = 'test'//or response.locals.test = 'test'

                        next()
                    })
                }
            }
        }
    ]
})

But in the static file endpoint I am always getting an empty locals object ({}): ((src/pages/)index).js:

export const get = async ({locals}) => {
    console.log(locals)

    return {body: ''}
}

Link to Minimal Reproducible Example

https://stackblitz.com/edit/github-z6jvzs?file=astro.config.js,src%2Fpages%2Findex.js

Participation

tony-sull commented 1 year ago

My initial thought here is that integrations shouldn't add middleware internally since the user wouldn't be able to control the order that middlewares are executed - I'd expect users to always add middlewares directly even if the middleware itself is imported from an NPM package

@ematipico I'm curious if you have any thoughts on how Astro.locals and middleware may work from inside of an integration while working on the RFC. Any ideas on if there's a safe way integrations could add middlewares?

ematipico commented 1 year ago

There's a third-party PR that would allow an adapter to receive and manipulate the Astro.locals: https://github.com/withastro/astro/pull/7049

But that's for adapters, not integrations.

The main issue I could see that we haven't managed to untangle is the ordering of the functions when creating the middleware.

The only solution I could see for integrations, is to expose a separate function that consumers can use inside the middleware.ts file.

ematipico commented 1 year ago

@kireerik that's not how you're supposed to use the Astro Middleware.

The middleware you use in your snippet, is the middleware provided by vite, which is totally unrelated to Astro Middleware.

kireerik commented 1 year ago

@ematipico I see. I am looking for a way to pass a function to a (static) Astro page endpoint from the referenced hook. Is there a way to do that?

(Currently I am using the global object as a workaround.)

ematipico commented 1 year ago

@ematipico I see. I am looking for a way to pass a function to a (static) Astro page endpoint from the referenced hook. Is there a way to do that?

(Currently I am using the global object as a workaround.)

Probably after merging this? https://github.com/withastro/astro/pull/7385

Otherwise, there isn't :(

kireerik commented 1 year ago

withastro/astro#7385 was released recently. So now it is possible somehow?

natemoo-re commented 11 months ago

I'm going to move this to our roadmap repo so we can discuss this, since it'd be a new feature.