Skip to content

Commit 48e44f8

Browse files
authored
Optimize page state to have smaller payload (#140)
1 parent 4c537d7 commit 48e44f8

File tree

7 files changed

+15
-8
lines changed

7 files changed

+15
-8
lines changed

app/assets/builds/@turbo-boost/commands.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/assets/builds/@turbo-boost/commands.js.map

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/javascript/drivers/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ function find(element) {
2323
invokeCommand: formDriver.invokeCommand
2424
}
2525

26-
if (turboMethod && turboMethod.length > 0)
26+
if (turboMethod?.length)
2727
return {
2828
name: 'method',
2929
reason: 'Element defines data-turbo-method.',

app/javascript/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function buildCommandPayload(id, element) {
3232
csrfToken: document.querySelector('meta[name="csrf-token"]')?.getAttribute('content'), // -- Rails CSRF token
3333
id, //-------------------------------------------------------------------------------------- Uniquely identifies the command invocation
3434
name: element.getAttribute(schema.commandAttribute), //------------------------------------- Command name
35-
elementId: element.id.length > 0 ? element.id : null, //------------------------------------ ID of the element that triggered the command
35+
elementId: element.id.length ? element.id : null, //---------------------------------------- ID of the element that triggered the command
3636
elementAttributes: elements.buildAttributePayload(element), //------------------------------ Attributes of the element that triggered the command
3737
startedAt: Date.now(), //------------------------------------------------------------------- Start time of when the command was invoked
3838
state: {

app/javascript/state/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ const save = () => {
2626
pages: { ...saved.pages }
2727
}
2828

29-
fresh.pages[location.pathname] = { ...fresh.pages[location.pathname], ...page.buildState() }
29+
// update the current page's state entry
30+
const pageKey = location.pathname
31+
const pageState = page.buildState()
32+
Object.values(pageState).length ? (fresh.pages[pageKey] = pageState) : delete fresh.pages[pageKey]
33+
3034
storage.save(key, fresh)
3135
}
3236

app/javascript/state/page.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ const buildState = () => {
1212
return elements.reduce((memo, element) => {
1313
const attributes = JSON.parse(element.getAttribute(schema.stateAttributesAttribute))
1414
if (element.id) {
15-
memo[element.id] = attributes.reduce((acc, name) => {
15+
const stateAttributes = attributes.reduce((acc, name) => {
1616
if (element.hasAttribute(name)) acc[name] = element.getAttribute(name) || name
1717
return acc
1818
}, {})
19+
if (Object.values(stateAttributes).length) memo[element.id] = stateAttributes
1920
}
2021
return memo
2122
}, {})

test/dummy/db/schema.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
# This file is auto-generated from the current state of the database. Instead
24
# of editing this file, please use the migrations feature of Active Record to
35
# incrementally modify your database, and then regenerate this schema definition.

0 commit comments

Comments
 (0)