nksaraf / vinxi

The Full Stack JavaScript SDK
https://vinxi.vercel.app
MIT License
1.89k stars 75 forks source link

Unable to load static assets in AWS Lambda #321

Closed Rizialdi closed 3 months ago

Rizialdi commented 3 months ago

Hi, I recently started playing around with solid-start and I use the starter code with:

// entry-client
import { mount, StartClient } from "@solidjs/start/client";

mount(() => <StartClient />, document.getElementById("app")!);
// entry-server
import { createHandler, StartServer } from "@solidjs/start/server";

export default createHandler(() => (
  <StartServer
    document={({ assets, children, scripts }) => (
      <html lang="en">
        <head>
          <meta charset="utf-8" />
          <meta name="viewport" content="width=device-width, initial-scale=1" />
          <link rel="icon" href="/favicon.ico" />
          {assets}
        </head>
        <body>
          <div id="app">{children}</div>
          {scripts}
        </body>
      </html>
    )}
  />
));

I learned about the fact that under the hood solid-start uses vinxi that is a smart way to combine vite and nitro. For the deployment I wanted to use terraform instead of solutions listed here and thanks to nitro preset I thought this could be as easy as:


import {defineConfig} from "@solidjs/start/config";

export default defineConfig({
  server: {
    preset: "aws-lambda",
  }
})

I did not really understand what to do with the outputs (public and server folder). Thanks to that guide I've use a Cloudfront distribution that points to S3 for static assets (example with favicon) and lambda for the rendering of the application but that is not working (Cannot read properties of undefined (reading 'file')).

I later found that defineNuxtConfig uses cdnURL for static assets but I was not able to find the same on vinxi.

// from https://towardsserverless.com/articles/nuxt3-ssr-on-aws-lambda
export default defineNuxtConfig({
  nitro: {
    preset: 'aws-lambda'
  },
  app: {
    cdnURL: 'https://example.cloudfront.net/'
  }
})

Do you guys have any ideas how to get the correct build for AWS Lambda with SSR and deliver static assets through a cdn ? For the lambda code I've only used the server folder (vinxi outputs). Do I need to use the public one as well ?

doeixd commented 3 months ago

I'd try server route rules https://nitro.unjs.io/guide/routing#route-rules

Rizialdi commented 3 months ago

More information on solid start repo