Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Roman Sarvarov committed Jun 15, 2024
1 parent b9cfac0 commit 4139d69
Show file tree
Hide file tree
Showing 37 changed files with 2,467 additions and 44,598 deletions.
46 changes: 6 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ This package based on the official Laravel adapter for Inertia.js: [inertiajs/in
- [x] Tests
- [x] Helpers for testing
- [x] Helpers for validation errors
- [x] Laravel like `mix` helper
- [x] Examples
- [ ] SSR

Expand Down Expand Up @@ -118,9 +117,7 @@ i, err := inertia.New(
i, err := inertia.New(
/* ... */
inertia.WithVersion("some-version"), // by any string
inertia.WithAssetURL("/build/assets/app.js?id=60a830d8589d5daeaf3d5aa6daf5dc06"), // by asset url
inertia.WithManifestFile("./public/manifest.json"), // by manifest file
inertia.WithMixManifestFile("./public/mix-manifest.json"), // by laravel-mix manifest file
inertia.WithVersionFromFile("./public/build/manifest.json"), // by file checksum
)
```

Expand Down Expand Up @@ -223,7 +220,7 @@ i.ShareTemplateFunc("trim", strings.Trim)
#### Pass template data via context (in middleware)

```go
ctx := i.WithTemplateData(r.Context(), "title", "Home page")
ctx := inertia.WithTemplateData(r.Context(), "title", "Home page")

// pass it to the next middleware or inertia.Render function using r.WithContext(ctx).
```
Expand All @@ -237,52 +234,21 @@ i.ShareProp("name", "Roman")
#### Pass props via context (in middleware)

```go
ctx := i.WithProp(r.Context(), "name", "Roman")
// or i.WithProps(r.Context(), inertia.Props{"name": "Roman"})
ctx := inertia.WithProp(r.Context(), "name", "Roman")
// or inertia.WithProps(r.Context(), inertia.Props{"name": "Roman"})

// pass it to the next middleware or inertia.Render function using r.WithContext(ctx).
```

#### Validation errors ([learn more](https://inertiajs.com/validation))

```go
ctx := i.WithValidationError(r.Context(), "some_field", "some error")
// or i.WithValidationErrors(r.Context(), inertia.ValidationErrors{"some_field": "some error"})
ctx := inertia.WithValidationError(r.Context(), "some_field", "some error")
// or inertia.WithValidationErrors(r.Context(), inertia.ValidationErrors{"some_field": "some error"})

// pass it to the next middleware or inertia.Render function using r.WithContext(ctx).
```

#### Laravel like `mix` helper

If you are familiar with Laravel, then you already know about awesome [mix](https://laravel.com/docs/8.x/mix#versioning-and-cache-busting) helper.

So, Gonertia also supports it:

```json
{
"/build/assets/app.js": "/build/assets/app.js?id=60a830d8589d5daeaf3d5aa6daf5dc06"
}
```

```go
i, err := inertia.New(
/* ... */
inertia.WithMixManifestFile("./public/mix-manifest.json"),
)
```

```html
<script type="module" src="{{ mix "/build/assets/app.js" }}"></script>
```

Render result will be:

```html
<script type="module" src="/build/assets/app.js?id=60a830d8589d5daeaf3d5aa6daf5dc06"></script>
```

Besides asset versioning, that will also support [Inertia's versioning](https://inertiajs.com/asset-versioning) using file checksum. Very convenient!

#### Testing

Of course, this package provides convenient interfaces for testing!
Expand Down
10 changes: 5 additions & 5 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const (
)

// WithTemplateData appends template data value to the passed context.Context.
func (i *Inertia) WithTemplateData(ctx context.Context, key string, val any) context.Context {
func WithTemplateData(ctx context.Context, key string, val any) context.Context {
if ctxData := ctx.Value(templateDataContextKey); ctxData != nil {
ctxData, ok := ctxData.(TemplateData)

Expand Down Expand Up @@ -46,7 +46,7 @@ func TemplateDataFromContext(ctx context.Context) (TemplateData, error) {
}

// WithProp appends prop value to the passed context.Context.
func (i *Inertia) WithProp(ctx context.Context, key string, val any) context.Context {
func WithProp(ctx context.Context, key string, val any) context.Context {
if ctxData := ctx.Value(propsContextKey); ctxData != nil {
ctxData, ok := ctxData.(Props)

Expand All @@ -62,7 +62,7 @@ func (i *Inertia) WithProp(ctx context.Context, key string, val any) context.Con
}

// WithProps appends props values to the passed context.Context.
func (i *Inertia) WithProps(ctx context.Context, props Props) context.Context {
func WithProps(ctx context.Context, props Props) context.Context {
if ctxData := ctx.Value(propsContextKey); ctxData != nil {
ctxData, ok := ctxData.(Props)

Expand Down Expand Up @@ -95,7 +95,7 @@ func PropsFromContext(ctx context.Context) (Props, error) {
}

// WithValidationError appends validation error to the passed context.Context.
func (i *Inertia) WithValidationError(ctx context.Context, key string, msg any) context.Context {
func WithValidationError(ctx context.Context, key string, msg any) context.Context {
if ctxData := ctx.Value(validationErrorsContextKey); ctxData != nil {
ctxData, ok := ctxData.(ValidationErrors)

Expand All @@ -111,7 +111,7 @@ func (i *Inertia) WithValidationError(ctx context.Context, key string, msg any)
}

// WithValidationErrors appends validation errors to the passed context.Context.
func (i *Inertia) WithValidationErrors(ctx context.Context, errors ValidationErrors) context.Context {
func WithValidationErrors(ctx context.Context, errors ValidationErrors) context.Context {
if ctxData := ctx.Value(validationErrorsContextKey); ctxData != nil {
ctxData, ok := ctxData.(ValidationErrors)

Expand Down
12 changes: 6 additions & 6 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
func TestInertia_WithTemplateData(t *testing.T) {
t.Parallel()

ctx := I().WithTemplateData(context.Background(), "foo", "bar")
ctx := WithTemplateData(context.Background(), "foo", "bar")

got, ok := ctx.Value(templateDataContextKey).(TemplateData)
if !ok {
Expand Down Expand Up @@ -80,7 +80,7 @@ func Test_TemplateDataFromContext(t *testing.T) {
func TestInertia_WithProp(t *testing.T) {
t.Parallel()

ctx := I().WithProp(context.Background(), "foo", "bar")
ctx := WithProp(context.Background(), "foo", "bar")

got, ok := ctx.Value(propsContextKey).(Props)
if !ok {
Expand All @@ -97,7 +97,7 @@ func TestInertia_WithProp(t *testing.T) {
func TestInertia_WithProps(t *testing.T) {
t.Parallel()

ctx := I().WithProps(context.Background(), Props{"foo": "bar"})
ctx := WithProps(context.Background(), Props{"foo": "bar"})

got, ok := ctx.Value(propsContextKey).(Props)
if !ok {
Expand Down Expand Up @@ -171,7 +171,7 @@ func TestInertia_WithValidationError(t *testing.T) {
t.Run("message is string", func(t *testing.T) {
t.Parallel()

ctx := I().WithValidationError(context.Background(), "foo", "bar")
ctx := WithValidationError(context.Background(), "foo", "bar")

got, ok := ctx.Value(validationErrorsContextKey).(ValidationErrors)
if !ok {
Expand All @@ -188,7 +188,7 @@ func TestInertia_WithValidationError(t *testing.T) {
t.Run("message is validation errors", func(t *testing.T) {
t.Parallel()

ctx := I().WithValidationError(context.Background(), "foo", ValidationErrors{
ctx := WithValidationError(context.Background(), "foo", ValidationErrors{
"abc": "123",
})

Expand All @@ -210,7 +210,7 @@ func TestInertia_WithValidationError(t *testing.T) {
func TestInertia_WithValidationErrors(t *testing.T) {
t.Parallel()

ctx := I().WithValidationErrors(context.Background(), ValidationErrors{"foo": "bar"})
ctx := WithValidationErrors(context.Background(), ValidationErrors{"foo": "bar"})

got, ok := ctx.Value(validationErrorsContextKey).(ValidationErrors)
if !ok {
Expand Down
41 changes: 41 additions & 0 deletions examples/vue3_tailwind/bootstrap/ssr/assets/Index-BR6qyg05.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { unref, withCtx, createVNode, useSSRContext } from "vue";
import { ssrRenderComponent, ssrRenderStyle, ssrInterpolate } from "vue/server-renderer";
import { Head } from "@inertiajs/vue3";
import { _ as _sfc_main$1 } from "./Navbar-D1KJP_4P.js";
import "@headlessui/vue";
import "@heroicons/vue/24/outline";
const _sfc_main = {
__name: "Index",
__ssrInlineRender: true,
props: {
text: String
},
setup(__props) {
return (_ctx, _push, _parent, _attrs) => {
_push(`<!--[-->`);
_push(ssrRenderComponent(unref(Head), null, {
default: withCtx((_, _push2, _parent2, _scopeId) => {
if (_push2) {
_push2(`<title${_scopeId}>Page 1</title>`);
} else {
return [
createVNode("title", null, "Page 1")
];
}
}),
_: 1
}, _parent));
_push(ssrRenderComponent(_sfc_main$1, null, null, _parent));
_push(`<div class="relative isolate px-6 pt-14 lg:px-8"><div class="absolute inset-x-0 -top-40 -z-10 transform-gpu overflow-hidden blur-3xl sm:-top-80" aria-hidden="true"><div class="relative left-[calc(50%-11rem)] aspect-[1155/678] w-[36.125rem] -translate-x-1/2 rotate-[30deg] bg-gradient-to-tr from-[#ff80b5] to-[#9089fc] opacity-30 sm:left-[calc(50%-30rem)] sm:w-[72.1875rem]" style="${ssrRenderStyle({ "clip-path": "polygon(74.1% 44.1%, 100% 61.6%, 97.5% 26.9%, 85.5% 0.1%, 80.7% 2%, 72.5% 32.5%, 60.2% 62.4%, 52.4% 68.1%, 47.5% 58.3%, 45.2% 34.5%, 27.5% 76.7%, 0.1% 64.9%, 17.9% 100%, 27.6% 76.8%, 76.1% 97.7%, 74.1% 44.1%)" })}"></div></div><div class="mx-auto max-w-2xl py-32 sm:py-48 lg:py-56"><div class="text-center"><h1 class="text-4xl font-bold tracking-tight text-gray-900 sm:text-6xl">Hello, ${ssrInterpolate(__props.text)}!</h1><p class="mt-6 text-lg leading-8 text-gray-600">Anim aute id magna aliqua ad ad non deserunt sunt. Qui irure qui lorem cupidatat commodo. Elit sunt amet fugiat veniam occaecat fugiat aliqua.</p><div class="mt-10 flex items-center justify-center gap-x-6"><a href="#" class="rounded-md bg-indigo-600 px-3.5 py-2.5 text-sm font-semibold text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600">Get started</a><a href="#" class="text-sm font-semibold leading-6 text-gray-900">Learn more <span aria-hidden="true">→</span></a></div></div></div><div class="absolute inset-x-0 top-[calc(100%-13rem)] -z-10 transform-gpu overflow-hidden blur-3xl sm:top-[calc(100%-30rem)]" aria-hidden="true"><div class="relative left-[calc(50%+3rem)] aspect-[1155/678] w-[36.125rem] -translate-x-1/2 bg-gradient-to-tr from-[#ff80b5] to-[#9089fc] opacity-30 sm:left-[calc(50%+36rem)] sm:w-[72.1875rem]" style="${ssrRenderStyle({ "clip-path": "polygon(74.1% 44.1%, 100% 61.6%, 97.5% 26.9%, 85.5% 0.1%, 80.7% 2%, 72.5% 32.5%, 60.2% 62.4%, 52.4% 68.1%, 47.5% 58.3%, 45.2% 34.5%, 27.5% 76.7%, 0.1% 64.9%, 17.9% 100%, 27.6% 76.8%, 76.1% 97.7%, 74.1% 44.1%)" })}"></div></div></div><!--]-->`);
};
}
};
const _sfc_setup = _sfc_main.setup;
_sfc_main.setup = (props, ctx) => {
const ssrContext = useSSRContext();
(ssrContext.modules || (ssrContext.modules = /* @__PURE__ */ new Set())).add("resources/js/Pages/Home/Index.vue");
return _sfc_setup ? _sfc_setup(props, ctx) : void 0;
};
export {
_sfc_main as default
};
87 changes: 87 additions & 0 deletions examples/vue3_tailwind/bootstrap/ssr/assets/Navbar-D1KJP_4P.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { mergeProps, unref, withCtx, createTextVNode, createVNode, useSSRContext } from "vue";
import { ssrRenderAttrs, ssrRenderComponent } from "vue/server-renderer";
import { Link } from "@inertiajs/vue3";
import { PopoverGroup } from "@headlessui/vue";
import { Bars3Icon } from "@heroicons/vue/24/outline";
const _sfc_main = {
__name: "Navbar",
__ssrInlineRender: true,
setup(__props) {
return (_ctx, _push, _parent, _attrs) => {
_push(`<header${ssrRenderAttrs(mergeProps({ class: "absolute inset-x-0 top-0 z-50" }, _attrs))}><nav class="mx-auto flex max-w-7xl items-center justify-between p-6 lg:px-8" aria-label="Global"><div class="flex lg:flex-1"><a href="#" class="-m-1.5 p-1.5"><span class="sr-only">Your Company</span><img class="h-8 w-auto" src="https://tailwindui.com/img/logos/mark.svg?color=indigo&amp;shade=600" alt=""></a></div><div class="flex lg:hidden"><button type="button" class="-m-2.5 inline-flex items-center justify-center rounded-md p-2.5 text-gray-700"><span class="sr-only">Open main menu</span>`);
_push(ssrRenderComponent(unref(Bars3Icon), {
class: "h-6 w-6",
"aria-hidden": "true"
}, null, _parent));
_push(`</button></div>`);
_push(ssrRenderComponent(unref(PopoverGroup), { class: "hidden lg:flex lg:gap-x-12" }, {
default: withCtx((_, _push2, _parent2, _scopeId) => {
if (_push2) {
_push2(ssrRenderComponent(unref(Link), {
href: "/home",
class: "text-sm font-semibold leading-6 text-gray-900"
}, {
default: withCtx((_2, _push3, _parent3, _scopeId2) => {
if (_push3) {
_push3(`Home`);
} else {
return [
createTextVNode("Home")
];
}
}),
_: 1
}, _parent2, _scopeId));
_push2(ssrRenderComponent(unref(Link), {
href: "/secondary",
class: "text-sm font-semibold leading-6 text-gray-900"
}, {
default: withCtx((_2, _push3, _parent3, _scopeId2) => {
if (_push3) {
_push3(`Secondary`);
} else {
return [
createTextVNode("Secondary")
];
}
}),
_: 1
}, _parent2, _scopeId));
} else {
return [
createVNode(unref(Link), {
href: "/home",
class: "text-sm font-semibold leading-6 text-gray-900"
}, {
default: withCtx(() => [
createTextVNode("Home")
]),
_: 1
}),
createVNode(unref(Link), {
href: "/secondary",
class: "text-sm font-semibold leading-6 text-gray-900"
}, {
default: withCtx(() => [
createTextVNode("Secondary")
]),
_: 1
})
];
}
}),
_: 1
}, _parent));
_push(`<div class="hidden lg:flex lg:flex-1 lg:justify-end"><a href="#" class="text-sm font-semibold leading-6 text-gray-900">Log in <span aria-hidden="true">→</span></a></div></nav></header>`);
};
}
};
const _sfc_setup = _sfc_main.setup;
_sfc_main.setup = (props, ctx) => {
const ssrContext = useSSRContext();
(ssrContext.modules || (ssrContext.modules = /* @__PURE__ */ new Set())).add("resources/js/Components/Navbar.vue");
return _sfc_setup ? _sfc_setup(props, ctx) : void 0;
};
export {
_sfc_main as _
};
41 changes: 41 additions & 0 deletions examples/vue3_tailwind/bootstrap/ssr/assets/Secondary-8nZPUrmp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { unref, withCtx, createVNode, useSSRContext } from "vue";
import { ssrRenderComponent, ssrRenderStyle } from "vue/server-renderer";
import { Head } from "@inertiajs/vue3";
import { _ as _sfc_main$1 } from "./Navbar-D1KJP_4P.js";
import "@headlessui/vue";
import "@heroicons/vue/24/outline";
const _sfc_main = {
__name: "Secondary",
__ssrInlineRender: true,
props: {
text: String
},
setup(__props) {
return (_ctx, _push, _parent, _attrs) => {
_push(`<!--[-->`);
_push(ssrRenderComponent(unref(Head), null, {
default: withCtx((_, _push2, _parent2, _scopeId) => {
if (_push2) {
_push2(`<title${_scopeId}>Page 2</title>`);
} else {
return [
createVNode("title", null, "Page 2")
];
}
}),
_: 1
}, _parent));
_push(ssrRenderComponent(_sfc_main$1, null, null, _parent));
_push(`<div class="relative isolate px-6 pt-14 lg:px-8"><div class="absolute inset-x-0 -top-40 -z-10 transform-gpu overflow-hidden blur-3xl sm:-top-80" aria-hidden="true"><div class="relative left-[calc(50%-11rem)] aspect-[1155/678] w-[36.125rem] -translate-x-1/2 rotate-[30deg] bg-gradient-to-tr from-[#ff80b5] to-[#9089fc] opacity-30 sm:left-[calc(50%-30rem)] sm:w-[72.1875rem]" style="${ssrRenderStyle({ "clip-path": "polygon(74.1% 44.1%, 100% 61.6%, 97.5% 26.9%, 85.5% 0.1%, 80.7% 2%, 72.5% 32.5%, 60.2% 62.4%, 52.4% 68.1%, 47.5% 58.3%, 45.2% 34.5%, 27.5% 76.7%, 0.1% 64.9%, 17.9% 100%, 27.6% 76.8%, 76.1% 97.7%, 74.1% 44.1%)" })}"></div></div><div class="mx-auto max-w-2xl py-32 sm:py-48 lg:py-56"><div class="text-center"><h1 class="text-4xl font-bold tracking-tight text-gray-900 sm:text-6xl">This is secondary page!</h1><p class="mt-6 text-lg leading-8 text-gray-600">Anim aute id magna aliqua ad ad non deserunt sunt. Qui irure qui lorem cupidatat commodo. Elit sunt amet fugiat veniam occaecat fugiat aliqua.</p></div></div><div class="absolute inset-x-0 top-[calc(100%-13rem)] -z-10 transform-gpu overflow-hidden blur-3xl sm:top-[calc(100%-30rem)]" aria-hidden="true"><div class="relative left-[calc(50%+3rem)] aspect-[1155/678] w-[36.125rem] -translate-x-1/2 bg-gradient-to-tr from-[#ff80b5] to-[#9089fc] opacity-30 sm:left-[calc(50%+36rem)] sm:w-[72.1875rem]" style="${ssrRenderStyle({ "clip-path": "polygon(74.1% 44.1%, 100% 61.6%, 97.5% 26.9%, 85.5% 0.1%, 80.7% 2%, 72.5% 32.5%, 60.2% 62.4%, 52.4% 68.1%, 47.5% 58.3%, 45.2% 34.5%, 27.5% 76.7%, 0.1% 64.9%, 17.9% 100%, 27.6% 76.8%, 76.1% 97.7%, 74.1% 44.1%)" })}"></div></div></div><!--]-->`);
};
}
};
const _sfc_setup = _sfc_main.setup;
_sfc_main.setup = (props, ctx) => {
const ssrContext = useSSRContext();
(ssrContext.modules || (ssrContext.modules = /* @__PURE__ */ new Set())).add("resources/js/Pages/Home/Secondary.vue");
return _sfc_setup ? _sfc_setup(props, ctx) : void 0;
};
export {
_sfc_main as default
};
13 changes: 13 additions & 0 deletions examples/vue3_tailwind/bootstrap/ssr/ssr-manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"node_modules/laravel-vite-plugin/inertia-helpers/index.js": [],
"resources/js/Components/Navbar.vue": [
"/build/assets/Navbar-D1KJP_4P.js"
],
"resources/js/Pages/Home/Index.vue": [
"/build/assets/Index-BR6qyg05.js"
],
"resources/js/Pages/Home/Secondary.vue": [
"/build/assets/Secondary-8nZPUrmp.js"
],
"resources/js/ssr.js": []
}
24 changes: 24 additions & 0 deletions examples/vue3_tailwind/bootstrap/ssr/ssr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { createSSRApp, h } from "vue";
import { renderToString } from "@vue/server-renderer";
import { createInertiaApp } from "@inertiajs/vue3";
import createServer from "@inertiajs/vue3/server";
async function resolvePageComponent(path, pages) {
for (const p of Array.isArray(path) ? path : [path]) {
const page = pages[p];
if (typeof page === "undefined") {
continue;
}
return typeof page === "function" ? page() : page;
}
throw new Error(`Page not found: ${path}`);
}
createServer(
(page) => createInertiaApp({
page,
render: renderToString,
resolve: (name) => resolvePageComponent(`./Pages/${name}.vue`, /* @__PURE__ */ Object.assign({ "./Pages/Home/Index.vue": () => import("./assets/Index-BR6qyg05.js"), "./Pages/Home/Secondary.vue": () => import("./assets/Secondary-8nZPUrmp.js") })),
setup({ App, props, plugin }) {
return createSSRApp({ render: () => h(App, props) }).use(plugin);
}
})
);
Loading

0 comments on commit 4139d69

Please sign in to comment.