Update - Angular 17.2 - 3/2/24
While you still can't get Vercel Edge deployments to work until the nodejs dependencies problem is resol...
For further actions, you may consider blocking this person and/or reporting abuse
Hey! Did you try it out with the new Angular 17 yet? They added integrated SSR support to Angular. Now running "ng serve" or "ng build" automatically starts SSR server, thats awesome!
Anyway, Vercel now throws the error:
The server folder looks way different now and has .mjs modules instead of .js.
Angular 16:
Angular 17:
I came up with this solution for the app/index.js:
Try with this on the import file:
And just keep all the package.json the same without editing it.
J
Ahhhh I totally forgot about the
api/index.js
and was wondering here he gets this server/main from ... :) Anyway I could NOT get it to work yet! I tried to all different kind of imports and renaming the file from index.js to index.mjs but I couldnt find the correct way yet. Maybe you have a look at it?OR
Nothing works...
Btw the package.json is now significantly different! Thats because
ng build
already builds browser and server app, so no more need forbuild:ssr
! This also means build/deploy time went from 2 minutes to 1 minute :)You have to use require because it is JS. Did you try my code? You're importing 'server.mjs'... hence
server/server
.I think thats the first thing tried but in mjs there is no require allowed anymore when I remember correct. I renamed index.js to index.mjs because it was not working.
It works doing what I said:
angular-ssr-vercel-hazel.vercel.app/
github.com/jdgamble555/angular-ssr...
I got it exactly like you, I double checked everything, I dont know what I am missing. Error:
My outputPath is just
dist
.Are you on Node 20 maybe? Because I cant use Node 20 because the version they use is not compatible with
@angular/animations
:EDIT: Nope, same error with Node 20 when I add
--ignore-engines
flag to yarn.Found it! The
outputPath
in angular.json actually need to bedist/something
and cant be justdist
. I dont know why but maybe it has something todo with how Vercel deploys the files.Doesnt work:
Works:
Thats an important info! Will you update the article to Angular 17 or write a new one?
I didn't change any settings. It defaults to Node 18. I also didn't edit package.json at all. Make sure you're using js and not ts. You can leave me your repo and I can look.
I am pretty sure that your example does not work, too:
angular-ssr-vercel-hazel.vercel.ap...
-> Your function is crashing. Probably with the same error as mine:
And the reason that you see a website on your example is:
If you want we can take a look together but how can I contact you? Or maybe you already find the reason why...
Btw I also see Vercel compiling my index.js:
You're right, but its not detecting my vercel.json file at all! Not sure why. It just loads the regular browser files and never runs the serverless function, and no errors.
Did you find anything out in the meantime?
@mickl @trongthuong96 @wolfsoko - Hey guys, I got it working thanks to all of your ideas. See the updated post with a demo.
Angular 17
A lot has changed :) Build times went down from 2:30 min to 1:00 min!
Please note that (according to my findings) it does NOT work if you set
outputPath
inangular.json
just todist
. It needs to bedist/my-project
, otherwise it does not work for some reason.package.json: No need to change anything, SSR is already included in Angular. If you are upgrading from Angular < 17 you can remove the
:ssr
scripts and thevercel-build
script:vercel.json:
api/index.js
What does your
package.json
look like?You dont change anything in Angular 17 SSR is already integrated:
"start": "ng serve",
"build": "ng build",
My function doesn't seem to be working, but I think Angular now works with ssr out of the box so it may be a moot point !? - angular-vercel-ssr.vercel.app/api
angular-vercel-ssr.vercel.app/
If you upgraded your project then use the angular.json from a fresh Angular 17 project because a lot has changed there!
Do you have a working link where the "api" folder that holds the function also works? I created a new project for testing. It seems to work out of the box (with no changes at all), but I think my function is not working. Is your function working when you navigate to
/api
?I was able to get it to work both ways
api
now forwarding to regular. See article changes for how. However, I think it may now work out-of-the-box if we don't do any changes at all.Yes it works fine. It redirects me to "/". If I disable JavaScript I see the prerendered pages. I see in you example you export
server.app()
while it should beserver.app
. Maybe thats the cause?I'm using a module to import it and it works. See the top of the article for the updates. Also, again, I think Angular SSR may work with Vercel now out of the box and none of this is necessary.
Why do you think it works out of the box? I tried removing vercel.json and api folder but it just shows 404 because build is now creating browser and server folder). If I go to /browser it displays the static index.html (without going through server first) and fails to load all the scripts.
@jdgamble555 I tried your code:
But for me it only works like this:
Did you add
"type": "module"
topackage.json
?Angular 17: file api/index.js:
Vercel.json:
Render on the home page:
Because by default the home page is not rendered. Don't know why { "source": "/(.*)", "destination": "/api" } doesn't automatically redirect to the home page.
(I used google translate)
It kinda works but for all static files (javascript, css, images) it returns the prerendered html page again. Does ist work for you?
It works with:
"includeFiles": "dist/MYPROJECT/**
I found a solution to work without redirecting the home page. Just rename index.html in src to index1.html. And in angular.json "index": "src/index1.html".
I dont have this problem! Just check my example and use the angular.json from a fresh Angular 17 project.
is it working now ssr on vercel?
Yes works fine!
Thanks for this! I made a few changes to my setup so I'm not always running production environments when I want to hit say staging backend.
Strip out
"vercel-build": "npm run build:ssr"
from package.jsonUpdate
"build:ssr"
to"ng build --configuration production && ng run project-name-here:server"
Create vercel.sh in your root directory of the project
The above is just looking at what branch is being deployed, if it's main/master build:ssr will run if its any other branch my staging-environment deployment script will run.
Settings on Vercel for the project add custom build command of
sh vercel.sh
Thanks man for this!! It has saved me, rsss. Thanks 4 share!
Thanks a lot for the article! Does this also work with Vercel Edge Runtime, which is just a subset of the Node API?
I have not tested it, but there is no reason it shouldn't work on the edge, as it is just JavaScript. You can try adding this to the
index.js
:vercel.com/docs/edge-network/caching
Let me know if you figure it out!
J
It would require to also set the response header as written in the docs BUT I tried it out and it doesnt work:
There is an open feature request to remove the dependencies of Angular Universal (e.g. remove Express) so it can work on workers that dont support full Node API like Vercel Edge Functions or Cloudflare Pages BUT it will probably be closed if it doesnt get 20 upvotes in the next week:
github.com/angular/angular-cli/iss...
Yup, I just tried it and go the same error. Normally in Node.js, you could use the "browser" key in package.json to ignore certain imports, but the edge uses Deno. Until this gets resolved (I upvoted BTW), I don't see Angular Universal being possible on Vercel or any Edge. However, AnalogJS may be a different thing.
Did you find anything out in the meantime?
I got it working thangs to @trongthuong96 ! I posted it on top level
Ok, great, could you post the working config?
Posted it on top level
Hi, I'm trying to push my Angular app (v16.2) to Vercel with SSR. I followed the steps and the deployment works fine. However, when I inspect the page source, I only see an
<app-root>
tag with nothing inside, indicating that the site isn't rendered server-side. I'm having trouble understanding why this is happening.Did you follow the index renaming and prerendering off part as well? I have also only tested on Angular 17.
Hey, this is my configuration but i am getting errors as show below.
api/index.js
vercel.json
package.json
using Angular V17
Please let me know on how to fix this. Thanks
Thanks for sharing this man! this is helpful
but one problem I am facing is my app is not server-rendered.
the home url is working but only the static version (with cookies disabled)
but any other url is taking more than 10 seconds to respond, resulting in 504 function timeout error.
could you please help my out?
Thanks
You may have a loop in your code. Make sure to test it with
npm run dev:ssr
first. Usually it is a login code that should only be ran on the frontend.why is your functions pointing to
dist/YOUR_PROJECT_NAME/browser/**
?Shouldn't it be from .../server/**?
No, the server is the index.js file. That is for the static files like images, css, etc
Thanks for this, How can i used with i18n ?
Not sure about that, never used i18n on any framework. Post here if you figure it out!
I do like this:
api/en/index.js
api/vi/index.js
vercel.json:
And angular.json
I don't know how to make it work with the default language (en) to use "baseHref": "/"
Thanks for the article!! One little thing: Instead of step 2 and 3 you could just go to the Vercel website and tell it to run "npm run build:ssr" or "yarn build:ssr".
Well you have to have the Vercel config file anyway, so this actually skips that step.
Using angular 17 ssr. This guide helped me a lot. Thank you.
Thank you!! That worked like a charm after I got 404 not found errors on Vercel.