Skip to content

[bug] edge server function monorepo environment variable #340

@lucasvieirasilva

Description

@lucasvieirasilva

Issue

When the Next.js project is created using a monorepo structure and deployed using the SST NextjsSite construct, the server function fails when the region where the user is located is different from the S3 cache bucket.

PermanentRedirect: The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint.

Cause

When OpenNext identifies a monorepo structure, it calls the addMonorepoEntrypoint function and creates a index.js exporting the handler inside the project package:

Source reference:
addMonorepoEntrypoint](https://github.com/sst/open-next/blob/3a5755fdcbfdf688cedc355e892b5ed74787993f/packages/open-next/src/build.ts#L712-L725)

SST adds the following code before the export * and the final code looks like:

process.env = { ...process.env, ..."{{ _SST_FUNCTION_ENVIRONMENT_ }}" };
export * from "./apps/frontend/admin-ui/index.mjs";

However, the way node.js works the imports run before the process.env block, the CACHE_BUCKET_REGION is not defined and the S3Client is defined without the right region:

https://github.com/sst/open-next/blob/3a5755fdcbfdf688cedc355e892b5ed74787993f/packages/open-next/src/adapters/server-adapter.ts#L33-L37

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions