calypso

calypso

Hello everyone
I would like to run my react app, which is built with webpack. Everything works fine.
But in the app.html.eex, I need to hardcode to css filename, which contains the contenthash generated by webpack. This contenthash can change and therefore I need to wildcard this to main.*.css.
Is this possible and if yes, how?

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <title>Test · Phoenix Framework</title>
    <script defer type="text/javascript" src="<%= Routes.static_path(@conn, "/main.js") %>"></script>
    <link rel="stylesheet" href="<%= Routes.static_path(@conn, "/main.68fc705c4c27d56f9522.css") %>"/>
  </head>
  <body>
  </body>
</html>

Best,
Calypso

Showing Posts 1 to 3

kokolegorille

kokolegorille

Are You using your own webpack outside of Phoenix?

This should be managed with mix phx.digest and You shouldn’t need to know the hash.

calypso

calypso OP

Thanks for the reply!

I’m using a custom webpack.config.ts, which is located in assets folder. Consequently, I configured in my dev.exs

config :app_web, AppWeb.Endpoint,
  http: [port: 4000],
  debug_errors: true,
  code_reloader: true,
  check_origin: false,
  watchers: [
    node: [
      "node_modules/webpack/bin/webpack.js",
      "--mode",
      "development",
      "--watch",
      "--watch-options-stdin",
      "--config",
      "webpack.dev.config.ts",
      cd: Path.expand("../apps/app_web/assets", __DIR__)
    ]
  ]

In the plugins section of the webpack.config.ts we see how the name of the .css is generated. Of course I can just change that to a static filename, that never changes like style.css. Then of course it is easy to load that file in the app.html.eex.

plugins: [
    new HtmlWebpackPlugin({
      template: "src/index.html",
    }),
    new HotModuleReplacementPlugin(),
    new MiniCssExtractPlugin({
        filename: "[name].[contenthash].css"
    }),
  ]

I do not quite get now mix phx.digest comes into play.

kokolegorille

kokolegorille

$ mix help phx.digest

                                 mix phx.digest                                 

Digests and compresses static files.

    mix phx.digest
    mix phx.digest priv/static -o /www/public

The first argument is the path where the static files are located. The -o
option indicates the path that will be used to save the digested and compressed
files.

If no path is given, it will use priv/static as the input and output path.

The output folder will contain:

  • the original file
  • the file compressed with gzip
  • a file containing the original file name and its digest
  • a compressed file containing the file name and its digest
  • a cache manifest file

Example of generated files:

  • app.js
  • app.js.gz
  • app-eb0a5b9302e8d32828d8a73f137cc8f0.js
  • app-eb0a5b9302e8d32828d8a73f137cc8f0.js.gz
  • cache_manifest.json
— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews