Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize page state to have smaller payload #140

Merged
merged 2 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/assets/builds/@turbo-boost/commands.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions app/assets/builds/@turbo-boost/commands.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion app/javascript/drivers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function find(element) {
invokeCommand: formDriver.invokeCommand
}

if (turboMethod && turboMethod.length > 0)
if (turboMethod?.length)
return {
name: 'method',
reason: 'Element defines data-turbo-method.',
Expand Down
2 changes: 1 addition & 1 deletion app/javascript/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function buildCommandPayload(id, element) {
csrfToken: document.querySelector('meta[name="csrf-token"]')?.getAttribute('content'), // -- Rails CSRF token
id, //-------------------------------------------------------------------------------------- Uniquely identifies the command invocation
name: element.getAttribute(schema.commandAttribute), //------------------------------------- Command name
elementId: element.id.length > 0 ? element.id : null, //------------------------------------ ID of the element that triggered the command
elementId: element.id.length ? element.id : null, //---------------------------------------- ID of the element that triggered the command
elementAttributes: elements.buildAttributePayload(element), //------------------------------ Attributes of the element that triggered the command
startedAt: Date.now(), //------------------------------------------------------------------- Start time of when the command was invoked
state: {
Expand Down
6 changes: 5 additions & 1 deletion app/javascript/state/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ const save = () => {
pages: { ...saved.pages }
}

fresh.pages[location.pathname] = { ...fresh.pages[location.pathname], ...page.buildState() }
// update the current page's state entry
const pageKey = location.pathname
const pageState = page.buildState()
Object.values(pageState).length ? (fresh.pages[pageKey] = pageState) : delete fresh.pages[pageKey]

storage.save(key, fresh)
}

Expand Down
3 changes: 2 additions & 1 deletion app/javascript/state/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ const buildState = () => {
return elements.reduce((memo, element) => {
const attributes = JSON.parse(element.getAttribute(schema.stateAttributesAttribute))
if (element.id) {
memo[element.id] = attributes.reduce((acc, name) => {
const stateAttributes = attributes.reduce((acc, name) => {
if (element.hasAttribute(name)) acc[name] = element.getAttribute(name) || name
return acc
}, {})
if (Object.values(stateAttributes).length) memo[element.id] = stateAttributes
}
return memo
}, {})
Expand Down
2 changes: 2 additions & 0 deletions test/dummy/db/schema.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
Expand Down
Loading