Skip to content

Commit

Permalink
Version 2.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
derrickreimer committed Jan 7, 2025
1 parent 9257896 commit 23ffaec
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 23 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## 2.1.0

### Fixed

- Include new Inertia v2 attributes in the initial page object (`mergeProps`, `deferredProps`, `encryptHistory`, `clearHistory`).
- Mark internal component functions in `Inertia.HTML` as private.

## 2.0.0

### Added
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ The package can be installed by adding `inertia` to your list of dependencies in
```elixir
def deps do
[
{:inertia, "~> 2.0.0"}
{:inertia, "~> 2.1.0"}
]
end
```
Expand Down
8 changes: 3 additions & 5 deletions lib/inertia/controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -308,15 +308,13 @@ defmodule Inertia.Controller do
only = if is_partial, do: conn.private[:inertia_partial_only], else: []
except = if is_partial, do: conn.private[:inertia_partial_except], else: []
camelize_props = conn.private[:inertia_camelize_props] || false
reset = conn.private[:inertia_reset] || []

props = Map.merge(shared_props, inline_props)
{props, merge_props} = resolve_merge_props(props)
{props, deferred_props} = resolve_deferred_props(props)

merge_props =
Enum.reject(merge_props, fn key ->
to_string(key) in conn.private[:inertia_reset]
end)
merge_props = Enum.reject(merge_props, fn key -> to_string(key) in reset end)

props =
props
Expand Down Expand Up @@ -492,7 +490,7 @@ defmodule Inertia.Controller do
defp send_csr_response(conn) do
conn
|> put_view(Inertia.HTML)
|> render(:inertia_page, inertia_assigns(conn))
|> render(:inertia_page, %{page: inertia_assigns(conn)})
end

defp inertia_assigns(conn) do
Expand Down
19 changes: 3 additions & 16 deletions lib/inertia/html.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,14 @@ defmodule Inertia.HTML do
"""
end

@doc type: :component
attr(:component, :string, required: true, doc: "The name of the JavaScript page component.")
attr(:props, :map, required: true, doc: "The page props (data).")
attr(:url, :string, required: true, doc: "The page URL.")
attr(:version, :string, required: true, doc: "The current asset version.")

@doc false
def inertia_page(assigns) do
~H"""
<div
id="app"
data-page={
json_library().encode!(%{component: @component, props: @props, url: @url, version: @version})
}
>
</div>
<div id="app" data-page={json_library().encode!(@page)}></div>
"""
end

@doc type: :component
attr(:body, :string, required: true, doc: "The server-rendered body")

@doc false
def inertia_ssr(assigns) do
~H"""
{Phoenix.HTML.raw(@body)}
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Inertia.MixProject do
use Mix.Project

@version "2.0.0"
@version "2.1.0"

def project do
[
Expand Down
23 changes: 23 additions & 0 deletions test/inertia_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,18 @@ defmodule InertiaTest do
test "processes deferred props on initial page load", %{conn: conn} do
conn =
conn
|> get(~p"/deferred_props")

body = html_response(conn, 200)
props = extract_page_data_from_html(body)

assert props["deferredProps"]["default"]
|> MapSet.new()
|> MapSet.equal?(MapSet.new(["a", "c"]))

conn =
conn
|> recycle()
|> put_req_header("x-inertia", "true")
|> put_req_header("x-inertia-version", @current_version)
|> get(~p"/deferred_props")
Expand Down Expand Up @@ -687,4 +699,15 @@ defmodule InertiaTest do
|> Phoenix.HTML.html_escape()
|> Phoenix.HTML.safe_to_string()
end

defp extract_page_data_from_html(raw_html) do
{:ok, html} = Floki.parse_document(raw_html)

[json_data] =
html
|> Floki.find("div[data-page]")
|> Floki.attribute("data-page")

Jason.decode!(json_data)
end
end

0 comments on commit 23ffaec

Please sign in to comment.