Skip to content

Fix 409 for missed pages #207

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

Merged
merged 3 commits into from
Apr 10, 2025
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
69 changes: 11 additions & 58 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config --exclude-limit 10000`
# on 2025-03-04 09:53:10 UTC using RuboCop version 1.73.2.
# on 2025-04-01 18:59:20 UTC using RuboCop version 1.75.1.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand Down Expand Up @@ -44,29 +44,12 @@ Layout/ExtraSpacing:
- 'spec/inertia/request_spec.rb'
- 'spec/inertia/response_spec.rb'

# Offense count: 1
# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: EnforcedStyle, IndentationWidth.
# SupportedStyles: consistent, consistent_relative_to_receiver, special_for_inner_method_call, special_for_inner_method_call_in_parentheses
Layout/FirstArgumentIndentation:
Exclude:
- 'spec/inertia/ssr_spec.rb'

# Offense count: 2
# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: EnforcedStyle, IndentationWidth.
# Configuration parameters: IndentationWidth.
# SupportedStyles: special_inside_parentheses, consistent, align_braces
Layout/FirstHashElementIndentation:
Exclude:
- 'spec/inertia/rspec_helper_spec.rb'

# Offense count: 4
# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: EnforcedStyle, IndentationWidth.
# SupportedStyles: aligned, indented, indented_relative_to_receiver
Layout/MultilineMethodCallIndentation:
Exclude:
- 'spec/inertia/ssr_spec.rb'
EnforcedStyle: consistent

# Offense count: 6
# This cop supports safe autocorrection (--autocorrect).
Expand Down Expand Up @@ -99,24 +82,20 @@ Layout/SpaceInsideBlockBraces:
- 'spec/inertia/error_sharing_spec.rb'
- 'spec/inertia/request_spec.rb'

# Offense count: 106
# Offense count: 56
# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBraces.
# SupportedStyles: space, no_space, compact
# SupportedStylesForEmptyBraces: space, no_space
Layout/SpaceInsideHashLiteralBraces:
Exclude:
- 'lib/inertia_rails/controller.rb'
- 'lib/inertia_rails/middleware.rb'
- 'spec/dummy/app/controllers/inertia_conditional_sharing_controller.rb'
- 'spec/dummy/app/controllers/inertia_share_test_controller.rb'
- 'spec/inertia/conditional_sharing_spec.rb'
- 'spec/inertia/middleware_spec.rb'
- 'spec/inertia/rails_mimic_spec.rb'
- 'spec/inertia/request_spec.rb'
- 'spec/inertia/rspec_helper_spec.rb'
- 'spec/inertia/sharing_spec.rb'
- 'spec/inertia/ssr_spec.rb'

# Offense count: 2
# This cop supports safe autocorrection (--autocorrect).
Expand Down Expand Up @@ -164,14 +143,6 @@ Lint/SymbolConversion:
Exclude:
- 'spec/inertia/rails_mimic_spec.rb'

# Offense count: 1
# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: AutoCorrect, AllowUnusedKeywordArguments, IgnoreEmptyMethods, IgnoreNotImplementedMethods, NotImplementedExceptions.
# NotImplementedExceptions: NotImplementedError
Lint/UnusedMethodArgument:
Exclude:
- 'lib/patches/mapper.rb'

# Offense count: 1
Naming/AccessorMethodName:
Exclude:
Expand All @@ -190,12 +161,12 @@ Naming/FileName:
# Offense count: 2
# Configuration parameters: EnforcedStyle, CheckMethodNames, CheckSymbols, AllowedIdentifiers, AllowedPatterns.
# SupportedStyles: snake_case, normalcase, non_integer
# AllowedIdentifiers: capture3, iso8601, rfc1123_date, rfc822, rfc2822, rfc3339, x86_64
# AllowedIdentifiers: TLS1_1, TLS1_2, capture3, iso8601, rfc1123_date, rfc822, rfc2822, rfc3339, x86_64
Naming/VariableNumber:
Exclude:
- 'spec/dummy/app/controllers/inertia_test_controller.rb'

# Offense count: 10
# Offense count: 7
# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: EnforcedStyle, ProceduralMethods, FunctionalMethods, AllowedMethods, AllowedPatterns, AllowBracesOnProceduralOneLiners, BracesRequiredMethods.
# SupportedStyles: line_count_based, semantic, braces_for_chaining, always_braces
Expand All @@ -210,12 +181,13 @@ Style/BlockDelimiters:
- 'spec/inertia/rails_mimic_spec.rb'
- 'spec/inertia/request_spec.rb'
- 'spec/inertia/rspec_helper_spec.rb'
- 'spec/inertia/sharing_spec.rb'

# Offense count: 1
# This cop supports unsafe autocorrection (--autocorrect-all).
# Configuration parameters: EnforcedStyle.
# Configuration parameters: EnforcedStyle, EnforcedStyleForClasses, EnforcedStyleForModules.
# SupportedStyles: nested, compact
# SupportedStylesForClasses: , nested, compact
# SupportedStylesForModules: , nested, compact
Style/ClassAndModuleChildren:
Exclude:
- 'lib/inertia_rails/helper.rb'
Expand Down Expand Up @@ -255,7 +227,7 @@ Style/ExpandPathArguments:
Exclude:
- 'spec/rails_helper.rb'

# Offense count: 69
# Offense count: 65
# This cop supports unsafe autocorrection (--autocorrect-all).
# Configuration parameters: EnforcedStyle.
# SupportedStyles: always, always_true, never
Expand Down Expand Up @@ -321,15 +293,11 @@ Style/FrozenStringLiteralComment:
- 'spec/inertia/conditional_sharing_spec.rb'
- 'spec/inertia/error_sharing_spec.rb'
- 'spec/inertia/lazy_prop_spec.rb'
- 'spec/inertia/middleware_spec.rb'
- 'spec/inertia/rails_mimic_spec.rb'
- 'spec/inertia/request_spec.rb'
- 'spec/inertia/response_spec.rb'
- 'spec/inertia/rspec_helper_spec.rb'
- 'spec/inertia/sharing_spec.rb'
- 'spec/inertia/ssr_spec.rb'
- 'spec/rails_helper.rb'
- 'spec/spec_helper.rb'
- 'spec/support/shared_examples.rb'

# Offense count: 1
Expand Down Expand Up @@ -399,11 +367,6 @@ Style/NestedModifier:
Exclude:
- 'lib/inertia_rails/controller.rb'

# Offense count: 1
Style/OpenStructUse:
Exclude:
- 'spec/inertia/ssr_spec.rb'

# Offense count: 2
# This cop supports safe autocorrection (--autocorrect).
Style/RedundantBegin:
Expand Down Expand Up @@ -456,7 +419,6 @@ Style/StringLiterals:
- 'lib/inertia_rails/controller.rb'
- 'lib/inertia_rails/engine.rb'
- 'lib/inertia_rails/helper.rb'
- 'lib/inertia_rails/middleware.rb'
- 'lib/inertia_rails/rspec.rb'
- 'lib/inertia_rails/version.rb'
- 'lib/patches/better_errors.rb'
Expand All @@ -476,7 +438,6 @@ Style/StringLiterals:
- 'spec/inertia/response_spec.rb'
- 'spec/inertia/rspec_helper_spec.rb'
- 'spec/rails_helper.rb'
- 'spec/spec_helper.rb'

# Offense count: 2
# This cop supports safe autocorrection (--autocorrect).
Expand Down Expand Up @@ -511,15 +472,7 @@ Style/TrailingCommaInHashLiteral:
- 'spec/inertia/response_spec.rb'
- 'spec/inertia/rspec_helper_spec.rb'

# Offense count: 1
# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: EnforcedStyle, MinSize, WordRegex.
# SupportedStyles: percent, brackets
Style/WordArray:
Exclude:
- 'lib/inertia_rails/middleware.rb'

# Offense count: 17
# Offense count: 24
# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns, SplitStrings.
# URISchemes: http, https
Expand Down
8 changes: 4 additions & 4 deletions lib/inertia_rails/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def redirect_status?(status)
end

def non_get_redirectable_method?
['PUT', 'PATCH', 'DELETE'].include? request_method
%w[PUT PATCH DELETE].include? request_method
end

def inertia_non_post_redirect?(status)
Expand All @@ -63,7 +63,7 @@ def get?
end

def controller
@env["action_controller.instance"]
@env['action_controller.instance']
end

def request_method
Expand All @@ -83,7 +83,7 @@ def version_stale?
end

def server_version
controller&.send(:inertia_configuration)&.version
(controller&.send(:inertia_configuration) || InertiaRails.configuration).version
end

def coerce_version(version)
Expand All @@ -92,7 +92,7 @@ def coerce_version(version)

def force_refresh(request)
request.flash.keep
Rack::Response.new('', 409, {'X-Inertia-Location' => request.original_url}).finish
Rack::Response.new('', 409, { 'X-Inertia-Location' => request.original_url }).finish
end

def copy_xsrf_to_csrf!
Expand Down
44 changes: 17 additions & 27 deletions lib/inertia_rails/renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@

module InertiaRails
class Renderer
KEEP_PROP = :keep
DONT_KEEP_PROP = :dont_keep

attr_reader(
:component,
:configuration,
Expand Down Expand Up @@ -82,31 +79,17 @@ def shared_data
#
# Functionally, this permits using either string or symbol keys in the controller. Since the results
# is cast to json, we should treat string/symbol keys as identical.
def merge_props(shared_data, props)
def merge_props(shared_props, props)
if @deep_merge
shared_data.deep_symbolize_keys.deep_merge!(props.deep_symbolize_keys)
shared_props.deep_symbolize_keys.deep_merge!(props.deep_symbolize_keys)
else
shared_data.symbolize_keys.merge(props.symbolize_keys)
shared_props.symbolize_keys.merge(props.symbolize_keys)
end
end

def computed_props
merged_props = merge_props(shared_data, props)

deep_transform_props merged_props do |prop, path|
next [DONT_KEEP_PROP] unless keep_prop?(prop, path)

transformed_prop = case prop
when BaseProp
prop.call(controller)
when Proc
controller.instance_exec(&prop)
else
prop
end

[KEEP_PROP, transformed_prop]
end
deep_transform_props(merged_props)
end

def page
Expand All @@ -128,16 +111,23 @@ def page
default_page
end

def deep_transform_props(props, parent_path = [], &block)
def deep_transform_props(props, parent_path = [])
props.each_with_object({}) do |(key, prop), transformed_props|
current_path = parent_path + [key]

if prop.is_a?(Hash) && prop.any?
nested = deep_transform_props(prop, current_path, &block)
transformed_props.merge!(key => nested) unless nested.empty?
else
action, transformed_prop = block.call(prop, current_path)
transformed_props.merge!(key => transformed_prop) if action == KEEP_PROP
nested = deep_transform_props(prop, current_path)
transformed_props[key] = nested unless nested.empty?
elsif keep_prop?(prop, current_path)
transformed_props[key] =
case prop
when BaseProp
prop.call(controller)
when Proc
controller.instance_exec(&prop)
else
prop
end
end
end
end
Expand Down
42 changes: 28 additions & 14 deletions spec/inertia/middleware_spec.rb
Original file line number Diff line number Diff line change
@@ -1,30 +1,52 @@
# frozen_string_literal: true

RSpec.describe InertiaRails::Middleware, type: :request do
context 'the version is stale' do
it 'tells the client to refresh' do
get empty_test_path, headers: {'X-Inertia' => true, 'X-Inertia-Version' => 'blkajdf'}
context 'the version is set' do
with_inertia_config version: '1.0'

it 'tells the client with stale version to refresh' do
get empty_test_path, headers: { 'X-Inertia' => true, 'X-Inertia-Version' => 'stale' }

expect(response.status).to eq 409
expect(response.headers['X-Inertia-Location']).to eq request.original_url
end

it 'returns page when version is up to date' do
get empty_test_path, headers: { 'X-Inertia' => true, 'X-Inertia-Version' => '1.0' }

expect(response.status).to eq 200
end

it 'returns response for non-inertia requests' do
get empty_test_path, headers: { 'X-Inertia-Version' => 'stale' }

expect(response.status).to eq 200
end

it 'returns 404 on unknown route' do
expect do
get '/unknown_route', headers: { 'X-Inertia' => true, 'X-Inertia-Version' => '1.0' }
end.to raise_error(ActionController::RoutingError)
end
end

context 'a redirect status was passed with an http method that preserves itself on 302 redirect' do
subject { response.status }

context 'PUT' do
before { put redirect_test_path, headers: {'X-Inertia' => true} }
before { put redirect_test_path, headers: { 'X-Inertia' => true } }

it { is_expected.to eq 303 }
end

context 'PATCH' do
before { patch redirect_test_path, headers: {'X-Inertia' => true} }
before { patch redirect_test_path, headers: { 'X-Inertia' => true } }

it { is_expected.to eq 303 }
end

context 'DELETE' do
before { delete redirect_test_path, headers: {'X-Inertia' => true} }
before { delete redirect_test_path, headers: { 'X-Inertia' => true } }

it { is_expected.to eq 303 }
end
Expand All @@ -47,12 +69,4 @@
expect(statusses.uniq).to eq([303])
end
end

context 'a request not originating from inertia' do
it 'is ignored' do
get empty_test_path, headers: {'X-Inertia-Version' => 'blkajdf'}

expect(response.status).to eq 200
end
end
end
Loading