Skip to content

Instantly share code, notes, and snippets.

@some1else
Last active March 1, 2024 12:36
Show Gist options
  • Save some1else/9ced1a40cd3469708e983208903f8651 to your computer and use it in GitHub Desktop.
Save some1else/9ced1a40cd3469708e983208903f8651 to your computer and use it in GitHub Desktop.
Vite & Rails: App layout for development & production
<!DOCTYPE html>
<html>
<head>
<title><%= request.host %></title>
<%= csrf_meta_tags %>
<%= csp_meta_tag %>
<%- unless ENV['index_js'].present? and ENV['index_css'].present?
Dir.glob("public/static/js/*.js").each do |file_path|
file_name = File.basename(file_path, File.extname(file_path))
ENV["index_js"] = "/static/js/#{file_name}.js"
end
Dir.glob("public/static/css/*.css").each do |file_path|
file_name = File.basename(file_path, File.extname(file_path))
ENV["index_css"] = "/static/css/#{file_name}.css"
end
end %>
<%- if Rails.env.production? %>
<link rel='stylesheet' href='<%= ENV["index_css"] %>' />
<%- end %>
<%- if Rails.env.development? %>
<script type="module">
import { injectIntoGlobalHook } from "http://<%= request.host %>:5173/@react-refresh";
injectIntoGlobalHook(window);
window.$RefreshReg$ = () => {};
window.$RefreshSig$ = () => (type) => type;
</script>
<script type="module" src="http://<%= request.host %>:5173/@vite/client"></script>
<%- end %>
</head>
<body class="<%= controller.controller_name %>-<%= controller.action_name %>">
<script>
<%- if Rails.env.production? %>
window.API_ROOT = 'https://<%= request.host %>';
<%- else %>
window.API_ROOT = 'http://<%= request.host %>:3000';
<%- end %>
</script>
<script>
window.user = <%= raw current_user.to_json %>
</script>
<%- flash.each do |name, msg| %>
<%= content_tag :div, msg, id: "flash_#{name}" %>
<%- end %>
<div id='root'>
<%= yield %>
</div>
<%- if Rails.env.production? %>
<script src="<%= ENV["index_js"] %>"></script>
<%- else %>
<script
type="module"
src="http://<%= request.host %>:5173/src/main.tsx?t=<%= Time.now.to_i %>"
>
</script>
<%- end %>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment