Deploying the Assets
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
With a Subscription, click any sentence in the script to jump to that part of the video!
Login SubscribeHow do we get our assets onto the site? If you "View Page Source", it looks like things are working. We see the importmap and... down here, these paths look correct: they even have the version part in their filenames.
Compiling Assets for Production
Unfortunately, all of these files return a 404. Boo. In the dev environment, when we're working locally, these files don't physically exist. But an internal Symfony listener intercepts the request, finds the file, and serves them.
But in the prod environment, that system isn't even active. It's too slow to run on production... so everything just 404s. And that's okay! A long time ago, we learned about the command to fix this:
php bin/console asset-map:compile
This command's job is simple: it takes all the assets that AssetMapper knows about and move them into the public/assets/ directory. It's not a command you need to run locally, but it is something you need to run when you deploy.
Copy this, head over to .platform.app.yaml, and go down to the build step. This is pretty cool! We're going to let Symfony do its build thing, and afterward, add our own stuff. Right here, add php bin/console asset-map:compile. That should do it!
Tip
Symfony + Platform.sh now detects AssetMapper and automatically runs this for you doing build. Woo!
| // ... lines 1 - 43 | |
| hooks: | |
| build: | | |
| // ... lines 46 - 49 | |
| NODE_VERSION=18 symfony-build | |
| php bin/console asset-map:compile | |
| // ... lines 53 - 58 |
Why are we running this during build and not deploy? As a rule of thumb, if a command's job is to "prepare" files, it should be in the build step. Or, another way to think about it is: if a command does not require a connection to the database or any other running services, there's a good chance it's a "build" thing.
Tip
Keep your "deploy" step as fast as possible because the incoming requests are held until it finishes. You can find more information here: https://docs.platform.sh/overview/build-deploy.html#deploy-steps
Head back over here and run:
git add -p
That whitespace bothers me... so I'll fix it and preserve my sanity. Then run git commit -m with a fancy message.
git commit -m "asset-map:compile"
You know what's next. Punch it!
symfony deploy
Let's fast-forward to the good part. Here it is! We see it running the command!
Compiling assets to
/app/public/assets/Compiled 16 assets
It also writes a few other files inside the public/assets/ directory: manifest.json and importmap.json. They help Symfony dump the importmap and other things onto the page even faster.
And... done! Spin over, refresh, and... it still looks bad!? Ah, but things are not as bad as you think! Head to the homepage and open your Console. Hey! Our JavaScript is running! We see the console.log()!
Building Tailwind on Deploy
So JavaScript, check! CSS... not so much. We still have a 404 on app.tailwind.css.
Remember: when you see a 404 to a filename that does not include a version part, like here, it means that AssetMapper can't find that file. Can you spot the problem? This app.tailwind.css is a file that we're building... and it's not committed to the repository! I'll stop this command and re-run it so we can see the details. Yup, we're building the app.tailwind.css file, ignoring it from Git, and since Platform.sh deploys using our files from Git, that file is simply missing.
No big deal. This is just another thing we need to add to our build step... before we run asset-map:compile so that the file is available.
I'll paste in the code for this. This is basically the same code we ran earlier to set things up locally, except that we're using the linux-x64 build. We're downloading that, moving it into the /bin directory (it doesn't really matter where it goes), making it executable, and then running that same command so that the output file is there by the time asset-map:compile runs.
| // ... lines 1 - 43 | |
| hooks: | |
| build: | | |
| // ... lines 46 - 51 | |
| curl -sLO https://github.com/tailwindlabs/tailwindcss/releases/download/v3.3.2/tailwindcss-linux-x64 | |
| mv tailwindcss-linux-x64 bin/tailwindcss | |
| chmod +x bin/tailwindcss | |
| ./bin/tailwindcss -i assets/styles/app.css -o assets/styles/app.tailwind.css | |
| // ... lines 56 - 63 |
Oh, and don't forget about the TailwindBundle which makes using Tailwind - including this deploy step - a bit easier.
Back over here, let's commit that new config change.... then deploy again:
symfony deploy
Even while it's deploying, we can see that this working. Last time, there were 16 files, now there are 17. When it finishes, spin over, refresh and... it's alive! All the pages have CSS. I love it!
Now that we're on production, let's talk about the things we need to check to make sure our assets are served blazingly fast.
11 Comments
Hi,
When I had deployed my project to Platform.sh it's styles didn't work until I changed the line in templates/base.html.twig:
After that all work well.
Did I do something wrong?
My build section in .platform.app.yaml looks like
Hey @Yuri-D
Your issue is you named the final CSS file
app.tailwind.cssthat's why it didn't work until you changed it in your template. You can keep it as is or update your deployment hookCheers!
How can I increase the execution time for the Tailwind build?
Hey Piotr,
It might be so that you need to increase PHP execution time, or what do you mean? You're not enough of default 30 seconds of execution? Could you provide more information on your specific use case, please?
Cheers!
When I run process cloud:deploy and when tailwind compiles, I get this error and the deploy process is interrupted.
Hey Piotr,
I see it now, thanks for sharing more information about it! Yeah, and I also noticed your issue here: https://github.com/SymfonyCasts/tailwind-bundle/issues/48
Seems you have a big project if you hit the timeout of 60s 🙈 But if your Tailwind project isn't that big, that sounds like the process somehow just hangs in the middle :/ I personally haven't hit that problem yet. Do you have some time to debug that case? Any PRs about fixes/improvements are warmly welcome on that repo!
Maybe try to run that specific command manually on your project, from the error message I suppose it should be:
I see you're on the latest v3.4.1 version. I wonder if before it worked for you and now the newest version causes this problem?
And try to stopwatch the time of execution. Does it really last more than 60s? Does it finish without errors?
Cheers!
Hello, I tried to run this command as profiles above, I even used 3 different versions of tailwindcss-linux-x64 but the compilation time was always very similar, around 41 seconds. I don't know why it is so long since my css file is only 80 lines long. Previously it took a fraction of a second.
I started removing some JS libraries and checking them. Well, I got down to 5 seconds, which still seems slow to me, maybe my config file is incorrect and it generates something that is completely unnecessary.
But anyway, everything is within the time limit now.
Thank you for your help.
PS.
But I still think that an optional increase in process execution time would be nice ;).
Hey Piotr,
Glad to hear you were able to make it working with a workaround. Thanks for sharing the solution with others. If more people encountered that problem - yeah, definitely something that would be nice to have out of the box
Cheers!
This not works for me.
I getting this error:
An I'm not sure how I can fix this.
my hooks section looks that:
Even if I will change
php bin/console tailwind:buildto:result it's this same.
Hey @piotrwasilewski!
Right now, using TailwindBundle with the platform.sh Symfony deploy is a bit annoying. It's something we need to improve (I know how, we just need to do it). The problem is that, inside of that
symfony-buildcommand, it runsimportmap:installthenasset-map:compile. But you don't have an opportunity to put thetailwind:buildin the middle of it.To work around this, you can tell the
symfony-buildscript not to do ANYTHING related to AssetMapper. Then run all the commands after on your own.Let me know if this helps!
Cheers!
Yes, that helps.
Thank you.
"Houston: no signs of life"
Start the conversation!