From 0079bebad91e1e13885e5b6900c9e363bdfc669e Mon Sep 17 00:00:00 2001 From: Derrick Reimer Date: Thu, 11 Jul 2024 12:33:56 -0500 Subject: [PATCH] Support unicode props --- CHANGELOG.md | 6 +++++ lib/inertia/ssr.ex | 2 +- test/inertia_test.exs | 22 +++++++++++++++++++ test/js/ssr.js | 4 ++-- .../my_app_web/controllers/page_controller.ex | 7 ++++++ test/support/my_app/lib/my_app_web/router.ex | 1 + 6 files changed, 39 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6f9a1f1..3ef0ff3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## 0.8.0 + +### Features + +- Support unicode props (by using the `binary` flag on Node function calls) + ## 0.7.0 ### Bug Fixes diff --git a/lib/inertia/ssr.ex b/lib/inertia/ssr.ex index a390a04..1064189 100644 --- a/lib/inertia/ssr.ex +++ b/lib/inertia/ssr.ex @@ -45,7 +45,7 @@ defmodule Inertia.SSR do @doc false def call(page) do module = GenServer.call(Config, :module) - NodeJS.call({module, :render}, [page], name: supervisor_name()) + NodeJS.call({module, :render}, [page], name: supervisor_name(), binary: true) end defp supervisor_name do diff --git a/test/inertia_test.exs b/test/inertia_test.exs index 207aced..df1df42 100644 --- a/test/inertia_test.exs +++ b/test/inertia_test.exs @@ -91,6 +91,28 @@ defmodule InertiaTest do assert body =~ ~s(
) end + + test "supports binary", %{conn: conn} do + path = + __ENV__.file + |> Path.dirname() + |> Path.join("js") + + start_supervised({Inertia.SSR, path: path}) + + Application.put_env(:inertia, :ssr, true) + + conn = + conn + |> get(~p"/binary_props") + + body = html_response(conn, 200) + + assert body =~ ~r/(\s*)New title(\s*)<\/title>/ + assert body =~ ~s(<meta name="description" content="Head stuff" />) + assert body =~ ~s(<div id="ssr">’</div>) + end + @tag :capture_log test "falls back to CSR if SSR fails and failure mode set to csr", %{conn: conn} do path = diff --git a/test/js/ssr.js b/test/js/ssr.js index c6c8d82..9b719ba 100644 --- a/test/js/ssr.js +++ b/test/js/ssr.js @@ -1,12 +1,12 @@ // A dummy module to simulate Inertia SSR rendering responses module.exports = { - render: (_page) => { + render: (page) => { return { head: [ `<title inertia>New title`, ``, ], - body: `
`, + body: `
${page.props.content || ""}
`, }; }, }; diff --git a/test/support/my_app/lib/my_app_web/controllers/page_controller.ex b/test/support/my_app/lib/my_app_web/controllers/page_controller.ex index 8fa5c11..c1111e4 100644 --- a/test/support/my_app/lib/my_app_web/controllers/page_controller.ex +++ b/test/support/my_app/lib/my_app_web/controllers/page_controller.ex @@ -87,6 +87,13 @@ defmodule MyAppWeb.PageController do |> render_inertia("Home") end + def binary_props(conn, _params) do + conn + |> assign(:page_title, "Home") + |> assign_prop(:content, "’") + |> render_inertia("Home") + end + def update(conn, _params) do conn |> put_flash(:info, "Updated") diff --git a/test/support/my_app/lib/my_app_web/router.ex b/test/support/my_app/lib/my_app_web/router.ex index daf5417..c8ccdf8 100644 --- a/test/support/my_app/lib/my_app_web/router.ex +++ b/test/support/my_app/lib/my_app_web/router.ex @@ -26,6 +26,7 @@ defmodule MyAppWeb.Router do get "/external_redirect", PageController, :external_redirect get "/overridden_flash", PageController, :overridden_flash get "/struct_props", PageController, :struct_props + get "/binary_props", PageController, :binary_props put "/", PageController, :update patch "/", PageController, :patch delete "/", PageController, :delete