Skip to content

Conversation

@charliecode
Copy link

Fixes #6331

Problem

When using await in edge.viewerRequest.injection, accessing routerNS inside getRoutes() throws a ReferenceError: cannot access variable before initialization due to temporal dead zone (TDZ) issues in the generated CloudFront Function code.

Solution

Moved the routerNS declaration inside the getRoutes() function where it's actually used. This prevents the TDZ issue when the injected code contains await statements.

Changes

  • Modified platform/src/components/aws/router.ts
  • Moved const routerNS = "${kvNamespace}"; from outside getRoutes() to inside it

Testing

  • Verified code compiles successfully with bun run build
  • The fix matches the workaround mentioned in the issue that resolves the problem

Copy link
Collaborator

@vimtor vimtor left a comment

Choose a reason for hiding this comment

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

thanks for the contribution @charliecode

proof of it working

tested this against the following config:

    const api = new sst.aws.Function("MyApi", {
      handler: "api.handler",
      url: true,
    });
    const router = new sst.aws.Router("MyRouter", {
      routes: {
        "/*": api.url,
      },
      edge: {
        viewerRequest: {
          injection: `
            // Test async injection - triggers issue #6331 if not fixed
            const testAwait = await Promise.resolve("test");
          `,
        },
      },
    });

    return {
      router: router.url,
    };

before

Image

after

Image

@vimtor vimtor self-assigned this Jan 18, 2026
@charliecode
Copy link
Author

thanks for the contribution @charliecode

proof of it working

Glad to help 🙌

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.

AWS Router: ReferenceError when viewerRequest injection uses await

2 participants