sst / ion

SST v3
https://sst.dev
MIT License
1.55k stars 198 forks source link

Remix/SsrSite: allow cloudFrontFunctions custom injections for serverCfFunction & staticCfFunction #866

Open ajaishankar opened 4 weeks ago

ajaishankar commented 4 weeks ago

Hi

I wanted to add a CloudFront function for Remix, and looks like it's very difficult (the following issue from astro)

https://github.com/sst/ion/issues/663#issuecomment-2247069996

For Remix looks like there is code that internally accepts an array of string injections

https://github.com/sst/ion/blob/dev/platform/src/components/aws/remix.ts#L545

return validatePlan({
  edge,
  cloudFrontFunctions: {
    serverCfFunction: {
      injections: [useCloudFrontFunctionHostHeaderInjection()],
    },
    staticCfFunction: {
      injections: [
        `request.uri = request.uri.split('/').map(encodeURIComponent).join('/');`,
      ],
    },
  },

Would it make sense to expose this in RemixArgs?

Then it would be very simple to add custom logic into both the serverCfFunction and staticCfFunction?

Something like

export interface RemixArgs extends SsrSiteArgs {
  cloudFrontFunctions: {
    serverCfFunction: { injections: string[] },
    staticCfFunction: { injections: string[] }
  },
  ...
}

Thanks!

jayair commented 3 weeks ago

How would you want to use these injections btw? Can you give me an example?

ajaishankar commented 3 weeks ago

Sure, this is say for home page personalization where the page is cached at the edge.

But based on a user cookie we can respond with a personalized homepage.

const useHomePagePersonalizationInjection = () => `
  var cookies = event.request.cookies;
  if(cookies.user && event.request.uri === "/") {
    request.querystring = "personalize=true";
  }
`

export default $config({
  async run() {
    new sst.aws.Remix("Web", {
      cloudFrontFunctions: {
        serverCfFunction: {
          injections: [ useHomePagePersonalizationInjection() ]
        }
      }
    })
  },
});