Monorepo (turborepo) with vite React frontend
bertvereecken
TRIALOP

a year ago

Hi

I have a monorepo setup with turborepo.
My monorepo contains two apps:

  • backend (hono nodeJS server)

  • admin dashboard (Vite React frontend).

I have two services in my Railway project

  • FE
    --- has a custom build command: pnpm build:frontend
    --- has a custom start command: pnpm start:frontend

  • BE
    --- has a custom build command: pnpm build:backend
    --- has a custom start command: pnpm start:backend

I have a question related to running my vite frontend.
I saw this template to deploy using Caddy.

I copied the Caddyfile + nixpacks.toml in my FE app, but I have the impression it's not doing anything or detecting it.

However, I do have a custom build and start command set. Maybe that might be the culprit.
I did deploy the React - caddy template and it worked fine.

So I am wondering if it's related to my monorepo setup or maybe something else.

Thanks for the help in advance.

Solved

0 Replies

bertvereecken
TRIALOP

a year ago

4838d5a0-e5bd-482c-bbf8-35740edea841


brody
EMPLOYEE

a year ago

is your frontend part of the shared repo? as in its importing stuff from the backend?


bertvereecken
TRIALOP

a year ago

@Brody backend and frontend app do not depend on each other. I made sure of that.


brody
EMPLOYEE

a year ago

isolated monorepo?


bertvereecken
TRIALOP

a year ago

1320846376896168000


bertvereecken
TRIALOP

a year ago

Backend imports some stuff from auth, db


bertvereecken
TRIALOP

a year ago

Frontend imports some stuff from ui


brody
EMPLOYEE

a year ago

gotcha


brody
EMPLOYEE

a year ago

I assume you already have two services in your railway project, one for the backend and one for the frontend?


bertvereecken
TRIALOP

a year ago

1320846744119935200


bertvereecken
TRIALOP

a year ago

yes


bertvereecken
TRIALOP

a year ago

1320846947191357400


bertvereecken
TRIALOP

a year ago

1320847092562002000


brody
EMPLOYEE

a year ago

perfect


brody
EMPLOYEE

a year ago

90% there


bertvereecken
TRIALOP

a year ago

damn it 😛


brody
EMPLOYEE

a year ago

set the location to the nixpacks.toml file with the NIXPACKS_CONFIG_FILE service variable, and then change your start command to start Caddy, you will likely need to adjust the file path for the Caddyfile in both the start command and fmt command within the nixpacks.toml


bertvereecken
TRIALOP

a year ago

Ok, before I do that I have a follow up question:
Is it the best and recommended way to run a vite react app with Caddy ? I saw in previous threads that some serve it with docker/node. Others were running with vite, but that was not recommended due to (very) high costs?


brody
EMPLOYEE

a year ago

the command vite runs a development server and that's going to be unstable and yes cost far more than a production grade web server such as Caddy


brody
EMPLOYEE

a year ago

a lot of people also use NGINX but I personally think Caddy is far more user friendly


bertvereecken
TRIALOP

a year ago

Thanks


bertvereecken
TRIALOP

a year ago

Trying your suggestion


brody
EMPLOYEE

a year ago

let me know if you run into any problems


bertvereecken
TRIALOP

a year ago

Ye


bertvereecken
TRIALOP

a year ago

NIXPACKSCONFIGFILE


bertvereecken
TRIALOP

a year ago

1320849846906916900


bertvereecken
TRIALOP

a year ago

probably these

1320849982575612200


brody
EMPLOYEE

a year ago

probably need to change the full path to the Caddyfile, given you have it in a subdirectory, you can edit that in the nixpacks.toml


brody
EMPLOYEE

a year ago

and then you'll want to remove the start command in the UI that you set so that the start command in the nixpacks.toml file will be used


bertvereecken
TRIALOP

a year ago

trying this now

1320850335324967000


bertvereecken
TRIALOP

a year ago

mmm, it deployed successfully, but FE is not reachable/timing out.

1320851684779491300


bertvereecken
TRIALOP

a year ago

Do i need to change something in the Caddyfile itself?

1320851780955017200


brody
EMPLOYEE

a year ago

no you shouldn't, does it result in a 502?


bertvereecken
TRIALOP

a year ago

Can't see if it's a 502


bertvereecken
TRIALOP

a year ago

1320855657976565800


bertvereecken
TRIALOP

a year ago

Wondering if this should be changed in the Caddyfile ?

1320856071987925000


bertvereecken
TRIALOP

a year ago

Tried it, but didn't solve it …


brody
EMPLOYEE

a year ago

nope, please do not change that


brody
EMPLOYEE

a year ago

your target port is likely set incorrectly


brody
EMPLOYEE

a year ago

well, don't change it unless you have some custom config that puts the built static files anywhere other than the default location


bertvereecken
TRIALOP

a year ago

No, default target is the dist folder


bertvereecken
TRIALOP

a year ago

Reverted the change 🙂


bertvereecken
TRIALOP

a year ago

1320857037927747600


bertvereecken
TRIALOP

a year ago

Probably this lol


brody
EMPLOYEE

a year ago

yep!


bertvereecken
TRIALOP

a year ago

Railway magic is magic 😄


brody
EMPLOYEE

a year ago

all working now?


bertvereecken
TRIALOP

a year ago

had a 404 when going to the URL, so did a re-deploy.


bertvereecken
TRIALOP

a year ago

Still 404


brody
EMPLOYEE

a year ago

you still have your build command set right?


bertvereecken
TRIALOP

a year ago

build command yes, start command is removed.


bertvereecken
TRIALOP

a year ago

Wondering if I need to set $PORT in variables ?

1320857902042316800


brody
EMPLOYEE

a year ago

maybe try updating the root to use the full path? I can't remember needing to do that before though


bertvereecken
TRIALOP

a year ago

which 'root' do you mean ?


brody
EMPLOYEE

a year ago

the root directory in the Caddyfile


bertvereecken
TRIALOP

a year ago

Will investigate further tomorrow


bertvereecken
TRIALOP

a year ago

Already a big thanks for the help


brody
EMPLOYEE

a year ago

no problem!


bertvereecken
TRIALOP

a year ago

I was testing with caddy locally.

I discovered the following:

When I run caddy locally from the root of the project/monorepo and run the command
caddy run --config apps/admin-dashboard/Caddyfile, then it's not accessible on PORT 3000 . When i go to 8080 I receive a 404 error.

When I navigate into the FE folder (admin-dashboard), and run the same command caddy run --config Caddyfile, it's the same issue (404).

But when i just run caddy run in the FE folder, than it works locally (without the --config)

Which leads me to believe there is something still misconfigured in the Caddyfile ?


bertvereecken
TRIALOP

a year ago

Ok, changing the root back to apps/admin-dashboard/Caddyfile fixed it. It's now up


bertvereecken
TRIALOP

a year ago

Now I am figuring out how to do a reverse proxy so that api calls to /api/products go to the backend service.


bertvereecken
TRIALOP

a year ago

I was able to figure out how to have the caddy reverse proxy working locally:

# global options
{
    admin off # theres no need for the admin api in railway's environment
    persist_config off # storage isn't persistent anyway
    auto_https off # railway handles https for us, this would cause issues if left enabled
    # runtime logs
    log {
        format json # set runtime log format to json mode
    }
    # server options
    servers {
        trusted_proxies static private_ranges 100.0.0.0/8 # trust railway's proxy
    }
}

# site block, listens on the $PORT environment variable, automatically assigned by railway
:{$PORT:3000} {
    # access logs
    log {
        format json # set access log format to json mode
    }

    # health check for railway
    rewrite /health /*

    handle /api/* {
        reverse_proxy localhost:3001
    }

    # Serve static files and handle client-side routing
    handle {
        # Serve files from the 'dist' folder
        root * apps/admin-dashboard/dist
        file_server

        # Rewrite all other requests to 'index.html' for client-side routing
        try_files {path} /index.html
    }

    # enable gzipping responses
    encode gzip
}

I had to do those two handle blocks, because try files was interupting my reverse proxy.
So it was always either my reverse proxy working and not my client side routing or the other way around.
By working with the two blocks, that got resolved.


bertvereecken
TRIALOP

a year ago

I tried deploying it to Railway. Client side routing works, but the call to the backend via the reverse proxy takes a long time when i change this line

Before

   handle /api/* {
        reverse_proxy localhost:3001
    }

After

   handle /api/* {
        reverse_proxy {$BACKEND_BASE_URL}
    }

bertvereecken
TRIALOP

a year ago

I have set the BACKEND_BASE_URL variable in my FE, but it doesn't work …

1321110070557278200


brody
EMPLOYEE

a year ago

may I ask why the reverse proxy is needed? simply have your frontend call the public domain of the backend, the reverse proxy is very likely needless complications


bertvereecken
TRIALOP

a year ago

Would've liked to hide my backend domain. But it's more of an optimization actually.


brody
EMPLOYEE

a year ago

I personally don't see that as an optimization, but as long as it's working!


brody
EMPLOYEE

a year ago

!s


Status changed to Solved brody 12 months ago


Loading...