Skip to content

Instantly share code, notes, and snippets.

@nikodunk
Last active May 10, 2024 15:24
Show Gist options
  • Save nikodunk/536a2261850c51c010b320eaf90b13cb to your computer and use it in GitHub Desktop.
Save nikodunk/536a2261850c51c010b320eaf90b13cb to your computer and use it in GitHub Desktop.
mounting remix build on hapi route

now at https://github.com/atmos-inc/api/pull/4930/files

yarn add @remix-run/node @mcansh/remix-raw-http @hapi/inert

cd web

remove { ssr: false } from vite.config.js

yarn build

move web/build/client/assets to /assets

// routes/app.mjs
import * as build from '../web/build/server/index.js'
import { createRequestHandler } from '@mcansh/remix-raw-http'

export default [
  {
    method: 'GET',
    path: '/{path*}',
    options: {
      auth: false
    },
    handler: (request, h) => {
      const handler = createRequestHandler({
        build,
        mode: build.mode
      })
      return handler(request.raw.req, request.raw.res)
    }
  },
  {
    method: 'GET',
    path: '/assets/{filename}',
    options: {
      auth: false
    },
    handler: (request, h) => {
      return h.file(`${request.params.filename}`)
    }
  }
]

Hapi route for mounting built remix app in prod, according to https://github.com/mcansh/remix-node-http-server/blob/main/example/server.js

// index.mjs

import hapiInert from '@hapi/inert'

...

files: {
        relativeTo: path.join(__dirname, 'web/build/client/assets')
      }
      
...

await server.register(hapiInert) // and remove realm

...
 
 if (file !== 'routes/app.mjs') {
        r.default.map(r => (r.path = `${config.server.api_path}${r.path}`))
      }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment