0

I am trying to deploy a Vite app on vercel which is using express to do SSR.

The express server runs successfully, vercel however doesn't deploy the built files (dist/server and dist/client, see the Vite SSR Guide)

I then changed the vercel build output configuration in hopes this will copy the files over:

{
  "version": 3,
  "cache": ["./api/*", "./dist/server/*", "./dist/client/*"]
}

Now it doesn't output any files at all except a static middleware file. enter image description here

How can I configure vercel to output the folders dist/server and dist/client which are created during the build step?

Really not finding any information on this online

1 Answer 1

0

I faced the same problem but found a solution (thanks to this article):

At first, it's important to state we are using this guide and this repository.

  1. Split your server.js into 2 files:
  • server.js

... // imports & constants (without port)

export const app = express();

... // server code

  • startServer.js

// Start http server
import { app } from './server.js';

const port = process.env.PORT || 5173;

app.listen(port, () => {
  console.log(`Server started at http://localhost:${port}`);
});

Then, change scripts in package.json:

  "scripts": {
    "dev": "node startServer",
    ...
    "preview": "cross-env NODE_ENV=production node startServer"
  }

This will allow you to still start your app locally.

  1. In your project directory, create a directory api and place there a file index.js containing:

import { app } from '../server.js';

export default app;

We did this to let Vercel start our server as a serverless function. Vercel cannot use app.listen().

  1. In your project directory, create a file vercel.json containing:

{
  "rewrites": [
    { "source": "/(.*)", "destination": "/api/index.js" }
  ],
  "functions": {
    "api/index.js": {
      "includeFiles": "dist/client/**"
    }
  }
}

rewrites forwards all requests to a serverless function. includeFiles allows to serve the directory and access files.

  1. Deploy as usual.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.