| 1 | package Mojolicious::Plugin::Inertia; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 8 | ▶ | use Mojo::Base 'Mojolicious::Plugin'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 8 | ▶ | use Mojo::JSON qw(encode_json); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 8 | ▶ | use Scalar::Util qw(reftype); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 8 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 8 | our $VERSION = "0.01"; | |||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 10 | ▶ | sub register { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
register: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 10 | my ($self, $app, $conf) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 11 | 10 | ▶ | $conf ||= {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$conf ||= {}
MC/DC0%0/1
$conf ||= {}
$conf
Truth table50%1/2
A
A= $conf
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 10 | ▶ | croak "Inertia plugin requires a 'version' configuration option" unless $conf->{version}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 9 | ▶ | croak "Inertia plugin requires a 'layout' configuration option" unless $conf->{layout}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | # Asset versioning. e.g. md5sum of your assets manifest file. | ||||||||||||||||||||||||||||||||||||||||||
| 17 | 8 | my $version = $conf->{version}; | |||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | # Layout template for non-Inertia requests. | ||||||||||||||||||||||||||||||||||||||||||
| 20 | # It must contain a <%= data_page %> placeholder at the application root. | ||||||||||||||||||||||||||||||||||||||||||
| 21 | 8 | ▶ | my $layout = ref $conf->{layout} ? $conf->{layout}->slurp : $conf->{layout}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | # History encryption settings (optional). | ||||||||||||||||||||||||||||||||||||||||||
| 24 | # Ref: https://inertiajs.com/history-encryption | ||||||||||||||||||||||||||||||||||||||||||
| 25 | 8 | ▶ | my $default_encrypt_history = defined $conf->{encrypt_history} ? $conf->{encrypt_history} : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | 8 | ▶ | my $default_clear_history = defined $conf->{clear_history} ? $conf->{clear_history} : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 15 | ▶ | $app->helper(inertia => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=14 SCAR=26.4
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | 15 | my ($c, $component, $props, $options) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 30 | 15 | ▶ | $props ||= {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$props ||= {}
MC/DC0%0/1
$props ||= {}
$props
Truth table50%1/2
A
A= $props
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | 15 | ▶ | $options ||= {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$options ||= {}
MC/DC100%1/1
$options ||= {}
$options
Truth table100%2/2
A
A= $options
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | # Options | ||||||||||||||||||||||||||||||||||||||||||
| 34 | 15 | ▶ | my $encrypt_history = defined $options->{encrypt_history} ? $options->{encrypt_history} : $default_encrypt_history; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | 15 | ▶ | my $clear_history = defined $options->{clear_history} ? $options->{clear_history} : $default_clear_history; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | # If the client's asset version does not match the server's version, | ||||||||||||||||||||||||||||||||||||||||||
| 38 | # then the client must do a full page reload. | ||||||||||||||||||||||||||||||||||||||||||
| 39 | # So, we respond with a 409 status and an X-Inertia-Location header | ||||||||||||||||||||||||||||||||||||||||||
| 40 | # Ref: https://inertiajs.com/the-protocol#asset-versioning | ||||||||||||||||||||||||||||||||||||||||||
| 41 | 15 | my $inertia_version = $c->req->headers->header('X-Inertia-Version'); | |||||||||||||||||||||||||||||||||||||||||
| 42 | 15 | ▶ | if ($c->req->method eq 'GET' && $inertia_version && $inertia_version ne $version) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$c->req->method eq "GET" and $inertia_version and $inertia_version ne $version
$c->req->method eq "GET" and $inertia_version
MC/DC66%2/3
$c->req->method eq "GET" and $inertia_version and $inertia_version ne $version
$c->req->method eq "GET"
$inertia_version
$inertia_version ne $version
Truth table75%3/4
A and B and C
A= $c->req->method eq "GET"B= $inertia_versionC= $inertia_version ne $version
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | 1 | $c->res->headers->header('X-Inertia-Location' => $c->req->url->to_string); | |||||||||||||||||||||||||||||||||||||||||
| 44 | 1 | return $c->rendered(409); | |||||||||||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | # Partial reloads allows you to request a subset of the props (data) from the server on subsequent visits to the same page component. | ||||||||||||||||||||||||||||||||||||||||||
| 48 | # Ref: https://inertiajs.com/the-protocol#partial-reloads | ||||||||||||||||||||||||||||||||||||||||||
| 49 | 14 | my $partial_data = $c->req->headers->header('X-Inertia-Partial-Data'); | |||||||||||||||||||||||||||||||||||||||||
| 50 | 14 | my $partial_component = $c->req->headers->header('X-Inertia-Partial-Component'); | |||||||||||||||||||||||||||||||||||||||||
| 51 | 14 | ▶ | if ($partial_data && $partial_component) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$partial_data and $partial_component
MC/DC50%1/2
$partial_data and $partial_component
$partial_data
$partial_component
Truth table66%2/3
A and B
A= $partial_dataB= $partial_component
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | 4 | my @only_keys = split /,/, $partial_data; | |||||||||||||||||||||||||||||||||||||||||
| 53 | 4 | $props = { map { $_ => $props->{$_} } @only_keys }; | |||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | # Resolve props that are coderefs by calling them with the current controller context. | ||||||||||||||||||||||||||||||||||||||||||
| 57 | # Code refs are useful for lazy loading data only when needed. | ||||||||||||||||||||||||||||||||||||||||||
| 58 | 14 | my $resolved_props = {}; | |||||||||||||||||||||||||||||||||||||||||
| 59 | 14 | for my $key (keys %$props) { | |||||||||||||||||||||||||||||||||||||||||
| 60 | 21 | my $prop = $props->{$key}; | |||||||||||||||||||||||||||||||||||||||||
| 61 | 21 | ▶ | $resolved_props->{$key} = (reftype($prop) || '') eq 'CODE' ? $prop->($c) : $prop; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
builtin::reftype($prop) || ''
MC/DC100%1/1
builtin::reftype($prop) || ''
builtin::reftype($prop)
Truth table100%2/2
A
A= builtin::reftype($prop)
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | # Construct the page object. | ||||||||||||||||||||||||||||||||||||||||||
| 65 | # Ref: https://inertiajs.com/the-protocol#the-page-object | ||||||||||||||||||||||||||||||||||||||||||
| 66 | 14 | my $page_object = { | |||||||||||||||||||||||||||||||||||||||||
| 67 | component => $component, | ||||||||||||||||||||||||||||||||||||||||||
| 68 | props => $resolved_props, | ||||||||||||||||||||||||||||||||||||||||||
| 69 | url => $c->req->url->to_string, | ||||||||||||||||||||||||||||||||||||||||||
| 70 | version => $version, | ||||||||||||||||||||||||||||||||||||||||||
| 71 | encryptHistory => $encrypt_history, | ||||||||||||||||||||||||||||||||||||||||||
| 72 | clearHistory => $clear_history, | ||||||||||||||||||||||||||||||||||||||||||
| 73 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | # Check if the request is an Inertia request. | ||||||||||||||||||||||||||||||||||||||||||
| 76 | # If so, return a JSON response. | ||||||||||||||||||||||||||||||||||||||||||
| 77 | # Else, return an HTML response with embedded page object. | ||||||||||||||||||||||||||||||||||||||||||
| 78 | # Ref: https://inertiajs.com/the-protocol#inertia-responses | ||||||||||||||||||||||||||||||||||||||||||
| 79 | 14 | my $is_inertia = $c->req->headers->header('X-Inertia'); | |||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | 14 | ▶ | if ($is_inertia) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | 11 | $c->res->headers->header('X-Inertia' => 'true'); | |||||||||||||||||||||||||||||||||||||||||
| 83 | 11 | $c->res->headers->header('Vary' => 'X-Inertia'); | |||||||||||||||||||||||||||||||||||||||||
| 84 | 11 | return $c->render(json => $page_object); | |||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||
| 86 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 87 | 3 | $c->res->headers->header('Vary' => 'X-Inertia'); | |||||||||||||||||||||||||||||||||||||||||
| 88 | 3 | return $c->render( | |||||||||||||||||||||||||||||||||||||||||
| 89 | inline => $layout, | ||||||||||||||||||||||||||||||||||||||||||
| 90 | format => 'html', | ||||||||||||||||||||||||||||||||||||||||||
| 91 | data_page => encode_json($page_object) | ||||||||||||||||||||||||||||||||||||||||||
| 92 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||
| 94 | 8 | }); | |||||||||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | 8 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 98 | __END__ | ||||||||||||||||||||||||||||||||||||||||||