How do you prevent phx.digest files from being commited with Phoenix 1.6?

Hi everyone

I was just wondering what ways you’ve come up with to prevent files created with mix phx.digest from being commited with the new default layout of Phoenix 1.6.

I.e. in Phoenix 1.6 static files like images etc go directly into the /priv/static folder by default, and only the generated css and js files in priv/static/assets are ignored in the .gitignore file. So if someone runs mix phx.digest in his development environment and then accidentially just runs git add .; git commit, all those digested files will be commited as well, something I personally find rather annoying.

My solution for now is that I’ve moved the static asset files to assets/static, and am then using watchexec to rsync all changes during development into priv/static. I’ve made a more detailed post about it if someone is interested.

However I’m not entirely happy with this approach, because I actually like the idea of simple layouts, i.e. files are in the same place in development like in production. So my open question is basically: Has anyone of you come up with an alternative to this? I’ve thought about using a git pre-commit hook as well, however that did not work as ex

pcected. Or maybe an entirely different idea?

Edit: Changed Topic/Category

1 Like

You could try configuring the pipeline to output in the same folders it used to be?

I honestly don’t know why this was changed tbh. I guess it has something to do with avoiding copying assets over to the priv folder. I also don’t recall reading about the process of changing it when Phoenix 1.6 came out, so as of right now I’ve been using Vite to replace Webpack because I found the transition easier than having almost no frontend pipeline out of the box.

1 Like

Yeah, that’s bascially what I did, as described in the blog post.

I was just wondering if someone had figured out a good setup where the files go directly into priv/static, maybe with a complicated pre-commit githook or something like that :slightly_smiling_face:

Could you just .gitignore root folder priv/static but include some files from it like this?

# Ignore priv/static root folder except some files
/priv/static
!/priv/static/favicon.ico
!/priv/static/robots.txt

/priv/static without / at the end should only ignore the actual folder not subfolders if I remember correctly.

Couldn’t you ignore just the *.gz files?

1 Like

No…

$ git st | grep static                                                                  priv/static/favicon-a8ca4e3a2bb8fea46a9ee9e102e7d3eb.ico
        priv/static/images/phoenix-5bd99a0d17dd41bc9d9bf6840abcc089.png
        priv/static/robots-067185ba27a5d9139b10a759679045bf.txt
        priv/static/robots-067185ba27a5d9139b10a759679045bf.txt.gz
        priv/static/robots.txt.gz
        priv/static/ui.html
1 Like

Hmm, had that idea as well, but for each .gz file a file with the digest is created as well. I.e. → for a file foo.txt we get foo-some-long-digest.txt and foo-some-long-digest.txt.gz.

Hmm, maybe I could find a regex that matches the digests…

Edit: Damn, saw the reply by NobbZ just now :slightly_smiling_face:

So only solution with way things currently work is to put all files that are not generated to a separate folder that you git include so something like this

/priv/static/
!/priv/static/content/
!/priv/static/favicon.ico
!/priv/static/robots.txt

and content folder would contain all files that are not generated.

Then you’d have to adjust your .gitignore file every time you need to add a new file to the static folder…

Just replied to Nobbz how it could work but needs you to put all non-generated files under a separate folder.

Maybe a solution, but the files inside content would still be digested…

Related Assets generated in folder which is not in .gitignore · Issue #4505 · phoenixframework/phoenix · GitHub

1 Like

According to the discussion on the issue #4505 referred by wanton7, we should run mix phx.digest.clean --all before running git add .; git commit.

If you run mix phx.digest in order to create a release, you can automate this process by a shell script like this:

mix phx.digest
mix release
mix phx.digest.clean --all
2 Likes

I’ve added

/priv/static/**/*.gz
/priv/static/**/*-????????????????????????????????.*
/priv/static/cache_manifest.json

to my .gitignore. It’s error-prone but seems to work for now.

5 Likes