承接 robertboes/sidecar-inertia-vite 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

robertboes/sidecar-inertia-vite

Composer 安装命令:

composer require robertboes/sidecar-inertia-vite

包简介

A Sidecar function to run Inertia server-side rendering with Vite on Lambda.

README 文档

README

🚨 Use this package with caution, as I'm still working on it

This package is based on hammerstonedev/sidecar-inertia. Aaron has done an amazing job with Sidecar and his original package lead me to figure out how to make it work with Vite.

Overview

This package provides a Sidecar function to run Inertia server-side rendering on AWS Lambda.

Sidecar packages, deploys, and executes AWS Lambda functions from your Laravel application.

Enabling SSR

Following the official Inertia docs on enabling SSR is a good place to start, but there are a few things you can skip:

  • You do not need to npm install @inertiajs/server
  • Come back here when you get to the "Building your application" section

I won't go into detail on how to setup SSR with Vite and Laravel, but the upcoming Inertia docs have more detailed information along with the Laravel docs

By default the SSR gateway is disabled, you can enable this by setting SIDECAR_INERTIA_VITE_ENABLED=true in your .env or by adjusting the configuration file to your liking.

Installation

To require this package, run the following:

composer require robertboes/sidecar-inertia-vite

This will install Sidecar as well.

Using the Sidecar Gateway

This package automatically overwrites the Inertia SSR gateway if the config sidecar-inertia-vite.ssr_gateway_enabled is true.

If for some reason the gateway isn't properly overwritten you can do this in your own service provider like so:

namespace App\Providers;

use RobertBoes\SidecarInertiaVite\SidecarGateway;
use Illuminate\Support\ServiceProvider;
use Inertia\Ssr\Gateway;

class AppServiceProvider extends ServiceProvider
{
    public function register()
    {
        // Use Sidecar to run Inertia SSR.
        $this->app->instance(Gateway::class, new SidecarGateway);
    }
}

Publishing Configuration

You can publish the configuration with the following command:

php artisan vendor:publish --provider="RobertBoes\SidecarInertiaVite\ServiceProvider" --tag="config"

Configuring Sidecar

If you haven't already, you'll need to configure Sidecar.

Publish the sidecar.php configuration file by running

php artisan sidecar:install

To configure your Sidecar AWS credentials interactively, you can run

php artisan sidecar:configure

The official Sidecar docs go into much further detail.

Now update your config/sidecar.php to include the function shipped with this package.

<?php

return [
    'functions' => [
        \RobertBoes\SidecarInertiaVite\SSRFunction::class,
    ],
    
    // ...
];

Updating Your JavaScript

This only covers Vue3, please follow the Inertia docs for Vue2 or React, and please open any issues.

This package assumes you're using a near-stock vite.config.js, which means the paths are defined by the Laravel plugin.

And update your resources/js/ssr.js to look something like this. The specifics may vary based on your application. If you're using Ziggy, you'll want to uncomment the Ziggy stuff. (This is based on the Inertia docs, with slight modifications.)

import {createSSRApp, h} from 'vue'
import {renderToString} from '@vue/server-renderer'
import {createInertiaApp} from '@inertiajs/inertia-vue3'
import { resolvePageComponent } from 'laravel-vite-plugin/inertia-helpers';
// import { ZiggyVue } from '../../vendor/tightenco/ziggy/dist/vue.m';

const appName = 'Laravel';

export async function handler(page) {
    return await createInertiaApp({
        page,
        render: renderToString,
        title: (title) => `${title} - ${appName}`,
        resolve: (name) => resolvePageComponent(`./Pages/${name}.vue`, import.meta.glob('./Pages/**/*.vue', { eager: false })),
        setup({ app, props, plugin }) {
            return createSSRApp({ render: () => h(app, props) })
                .use(plugin)
                // .use(ZiggyVue, {
                //     ...page.props.ziggy,
                //     location: new URL(page.props.ziggy.location),
                // })
        },
    })
}

Bundling / node_modules

By default the config option sidecar-inertia-vite.bundle is set to true. This will bundle your SSR function using @vercel/ncc, which produces a single file and doesn't require the inclusion of the node_modules folder. Using this approach it will generate a single index.mjs file along with dynamically imported modules.

If you wish to not use this you can set the option to false. This will create a Lambda function with roughly the following contents:

- assets/
- node_modules/
- ssr.mjs

Deploying Your SSR Function

After you have added the SSR function to your sidecar.php, you should run php artisan sidecar:deploy --activate to deploy your function.

This will compile your JavaScript for you as a beforeDeployment hook, so you don't have to worry about remembering to do that first.

Debugging SSR

It's recommended that you deploy your Sidecar function locally so that you can test SSR more quickly. You can run php artisan sidecar:deploy --activate from your local machine and your SSR function will be deployed to Lambda.

You can also set debug to true in your config/sidecar-inertia-vite.php file, so that Sidecar will throw exceptions when SSR fails instead of falling back to client-side rendering. This will help you diagnose issues quickly.

A note on Ziggy

I personally don't use Ziggy, but I did try this in a Laravel Jetstream application and everything seemed to work fine. If anything doesn't work or the docs need more explanation, feel free to submit a PR.

robertboes/sidecar-inertia-vite 适用场景与选型建议

robertboes/sidecar-inertia-vite 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.74k 次下载、GitHub Stars 达 18, 最近一次更新时间为 2022 年 12 月 08 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 robertboes/sidecar-inertia-vite 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 robertboes/sidecar-inertia-vite 我们能提供哪些服务?
定制开发 / 二次开发

基于 robertboes/sidecar-inertia-vite 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.74k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 18
  • 点击次数: 18
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 18
  • Watchers: 2
  • Forks: 10
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-12-08