Comments (9)
oh, I think I missed something re-reading this thread. If I check in the generated go code from when templ was running in watch mode it's going to make reference to this function.
I had checked it in from a previous issue I was reading mentioning it was idiomatic to commit the generated code so that people who don't have the templ built tools installed ahead of time can build the application without worrying about that step of the build chain.
from templ.
Adding to this:
Here's the error I'm getting:
2024-02-09T17:08:46Z ERR server/server.go:23 > 200 | GET: '/' -- templ: failed to cache strings: templ: failed to open /app/views/layout/base_templ.txt: open /app/views/layout/base_templ.txt: no such file or directory pid=1
EDIT: I had to port over my views folder for the .txt files to be there in my dockerfile. I don't know if this is intentional. But perhaps this can be added in the documentation for projects with components in nested folders?
from templ.
Before the build step in Docker you need to run temple generate
it's copying over your --watch
mode templates.
from templ.
Before the build step in Docker you need to run
temple generate
it's copying over your--watch
mode templates.
Oh I see. So I would need to build and install templ in my docker image, and then templ generate
to get the txt files during my build stage? Is this the best way? seems a bit excessive haha Also, for some reason, when I do templ generate.. It doesn't generate the txt files anymore. Only templ generate --watch does and it deletes the txt file when I stop it
from templ.
The txt files are only relevant for dev mode. When you run templ generate on it's own you get the production templates. That's why I'd recommend a templ generate in your build process, just incase you forgot to do a prod build.
from templ.
So I would need to build and install templ in my docker image, and then
templ generate
to get the txt files during my build stage? Is this the best way? seems a bit excessive
I feel like it's better to do it this way imo. When I personally use templ I gitignore all of the Go code that templ generates (all *_templ.go
files) and the docker image runs templ generate
everytime so it always has the most up-to-date code. This also will fail the docker build if templ throws an error during generation which is better because it ensures the templ code I pushed is still valid.
Here's an snippet from one of my projects:
# BASE IMAGES
FROM --platform=$BUILDPLATFORM golang:1.21 as go-base
# GENERATE TEMPL
FROM go-base as templ-builder
WORKDIR /app
COPY go.mod .
# install templ version that go.mod is using
RUN go install github.com/a-h/templ/cmd/templ@$(go list -m -f '{{ .Version }}' github.com/a-h/templ)
COPY web/templ web/templ
RUN templ generate --path=web/templ
from templ.
So I would need to build and install templ in my docker image, and then
templ generate
to get the txt files during my build stage? Is this the best way? seems a bit excessiveI feel like it's better to do it this way imo. When I personally use templ I gitignore all of the Go code that templ generates (all
*_templ.go
files) and the docker image runstempl generate
everytime so it always has the most up-to-date code. This also will fail the docker build if templ throws an error during generation which is better because it ensures the templ code I pushed is still valid.Here's an snippet from one of my projects:
# BASE IMAGES FROM --platform=$BUILDPLATFORM golang:1.21 as go-base # GENERATE TEMPL FROM go-base as templ-builder WORKDIR /app COPY go.mod . # install templ version that go.mod is using RUN go install github.com/a-h/templ/cmd/templ@$(go list -m -f '{{ .Version }}' github.com/a-h/templ) COPY web/templ web/templ RUN templ generate --path=web/templ
Awesome thanks for this, I'll use it as a reference for mine. Having thought about it, it does make sense to do it on build to simplify the CI/CD process
from templ.
Just a little inquiry on this, the error failing explicitly says "failed to cache strings", and it looks like that's being generated by WriteWatchModeString
// WriteWatchModeString is used when rendering templates in development mode.
// the generator would have written non-go code to the _templ.txt file, which
// is then read by this function and written to the output.
based on the documentation, this shouldn't be getting called at all, right? this isn't dev mode -- it's likely production.
is the documentation misleading, or is this function getting called accidentally?
especially given an app is probably running in a hardened container, it shouldn't be writing anything out to the filesystem (and in many cases doesn't have permission to at all).
from templ.
What is the purpose of the text files when developing locally? Seems like it adds more complications than benefit. For instance, even if you choose to gitignore them AND also decide to take the idiomatic approach to track the generated go files, it would be very easy during development to stage and commit all your files to git (in the middle of your work) without first stopping the templ generate --watch
command. This would then lead to a problem state where the generated go files you committed are referencing the txt files you chose to ignore...
In my opinion @alehechka has a good point to ignore them both and run the generate command in the CI/CD (or manual build) process. It's not as convenient as @adasauce points out, but if someone is building and running the app for development, they will probably need to install templ cli at some point anyways right?
Lastly, I think it would be helpful to update the docs on the website to discuss gitignore and deployment best practices more clearly. I ended up on this issue because I could not find a mention of if the generated text files should be tracked or not in the repository. I looked at the docker example also and noticed that the generated go files are tracked and that is also does not have a .gitignore file at all.
from templ.
Related Issues (20)
- -stdout sends error report to output file HOT 3
- bug: script elements don't render script templates
- lsp: support the organizeImports code action HOT 3
- Wasm support for interacting with the DOM HOT 3
- generator: can't use iterator value in for loop HOT 8
- max retries reached HOT 3
- lsp: panic on codeAction HOT 6
- question: Escape go keywords at the beginning of the line HOT 3
- generator: Duplicate Import Statements in Generated Templates Cause go build Failure HOT 5
- vscode: folded files in tree view seem to not work when rust-analyzer is installed HOT 2
- Issue: Unexpected Script Placement in Generated HTML HOT 3
- fmt: <pre> tags not handled correctly HOT 3
- parser: style attribute of DOM element doesn't accept dynamic styles HOT 1
- proposal: Typed Children for Wrappers HOT 2
- Proposal: Allow spreading List of `templ.Components`, similar to `{ children ... }` HOT 1
- css expression: found unexpected parameters HOT 2
- generator: Add nonce support for style tags
- Abandoned change to not modify files where fmt makes no changes HOT 3
- Proposal: Load file as templ HOT 4
- Help with intercepting middleware, and writing responses HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from templ.