npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ocelotlstudio/laranuxt

v1.0.0

Published

Nuxt cli extension for use with laravel as backend

Downloads

1

Readme

Nuxt Laravel

Commitizen friendly npm

This module makes it easy to integrate a NuxtJS SPA into a Laravel application.
The implementation is based on laravel-nuxt-js by skyrpex.

This fork updates dependencies and enables option for compiling in multiples enviroments through .env files.

There is a companion extension also based on skyrpex's work, which makes it very easy to set up nuxt inside an existing laravel project: m2s/laravel-nuxt

Hint: Use the companion extension for routing integration with laravel.

Features

  • Easily deploy an existing Nuxt app inside a Laravel application or vice versa
  • Test your Nuxt app with live reloading, HMR and the auto-configured Laravel test server
  • Seamlessly integrate Nuxt into the URL resolution of Laravel
  • Easy integration with sanctum

Setup

Installation

Install this package.

npm install --save-dev @ocelotlstudio/laranuxt

or

yarn add --dev @ocelotlstudio/laranuxt

Typescript

To have code completion/type checking on the Configuration interface from @nuxt/types, include the package in your tsconfig.json.

{
  "compilerOptions": {
    // ...
    "types": [
        "@nuxt/types",
        // ...
        "@ocelotlstudio/laranuxt"
    ]
  }
}

Configuration

Simply include nuxt-laravel in modules and set the ssr setting to false in your nuxt.config.js

export default {
  ssr: false,
  modules: [
    // Include it first, so that configuration alterations are propagated to other modules
    '@ocelotlstudio/laranuxt'
    // ... other modules
  ]
}

If your package.json lives in the Laravel root folder you are done.

Otherwise set the path to your Laravel root folder through the configuration.

export default {
  ssr: false,
  modules: [
    '@ocelotlstudio/laranuxt'
  ],
  laravel: {
    root: './path/to/laravel'
  }
}

Module Options

| option | type | description | default | | -------------- | --------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------- | | root | string | Path to laravel directory (is resolved relative to process.cwd()) | process.cwd() | | publicDir | string | The folder where laravel serves assets from (is resolved relative to root) | 'public' | | outputPath | string | File location to which an additional index route will be rendered, useful if you want to store it in a folder outside of Laravels public dir (is resolved relative to root) | null | | server | boolean or object | Settings for the Laravel testserver | (see below) | | dotEnvExport | boolean | Whether the NUXT_OUTPUT_PATH varibale should be written to the .env file in the laravel root directory | false | | envFile | string | .env file name, useful if you want to use multiple enviroment settings | null |

The module loads the default .env or envFile specified from your laravel root, so you can set the NUXT_OUTPUT_PATH environment variable from there.

The server setting

If this setting is set to false the module will be disabled for development.
Setting this to true is equivalent to omitting it and will simply use the default configuration.

| option | type | description | default | | ------ | -------- | --------------------------- | ---------------------------- | | host | string | Hostname for the testserver | nuxtConfig.server.host | | port | number | Port for the testserver | nuxtConfig.server.port + 1 |

Path resolution inside publicDir

If nuxtConfig.router.base is not set the SPA will be generated in the publicDir root with an index file name of spa.html.
If nuxtConfig.router.base is set the SPA will be generated in a corresponding location inside publicDir with the default index file name index.html.

Laravel integration

Laravel integration is accomplished through two environment variables.

  • APP_URL:
    Laravel uses this to generate asset URLs.

    • When the Laravel test server is started through this module this variable is overwritten with the nuxt test server URL origin via putenv.
  • NUXT_OUTPUT_PATH:
    Use this variable to redirect all web traffic to, which you want handled by nuxt.

    • When the Laravel test server is started through this module this variable is overwritten with a special index route on the nuxt test server via putenv.
    • When nuxt is build through this module (and dotEnvExport is truthy) this variable will be written to the .env file in laravels root directory, containing the resolved outputPath (see above).

❗❗❗ Attention ❗❗❗:
Make sure your putenv is in the disabled_functions in your php.ini
and that putenv support is enabled for the laravel env() helper.

Alternatively (still: if putenv is enabled in PHP) you can just use the getenv() function directly.
If you want to use putenv directly you should update your config/app.php to get APP_URL that way.

Example scaffolding in existent Laravel application

The easy way

  1. Install m2s/laravel-nuxt:

    composer require m2s/laravel-nuxt
  2. Execute the install command (<source> can be omitted and defaults to resources/nuxt)

    php artisan nuxt:install <source>

The hard (all configuration in project root) way

  1. Create a new nuxt app in resources/nuxt

    npx create-nuxt-app resources/nuxt
  2. Migrate all dependencies and scipts (most importantly dev and build) from resources/nuxt/package.json into package.json in Laravel root and delete it

  3. Move all configuration files from resources/nuxt to Laravel root (or merge where appropiate, e.g. .editorconfig)

  4. Install the module and it's peer dependencies

    npm i -D @ocelotlstudio/nuxt-laravel @nuxtjs/axios @nuxtjs/proxy
  5. Update nuxt.config.js

    module.exports = {
      srcDir: 'resources/nuxt',
      ssr: false,
      // ... other config
      modules: [
        '@ocelotlstudio/laranuxt',
        // ... other modules
      ]
    }
  6. (Optional) If you use jest, or other tools that reference the Nuxt root independently, you have to update thier respective configuration to make them work correctly.
    Example jest.config.js:

    module.exports = {
      rootDir: 'resources/nuxt',
      // ... other configurtion
    }

Example Laravel configuration

Forwarding all undefined routes to nuxt

routes/web.php:

// ...
// Add this route last as a catch all for undefined routes.
Route::get(
    '/{path?}',
    function($request) {
      // ...
      // If the request expects JSON, it means that
      // someone sent a request to an invalid route.
      if ($request->expectsJson()) {
          abort(404);
      }

      // Fetch and display the page from the render path on nuxt dev server or fallback to static file
      return file_get_contents(env('NUXT_OUTPUT_PATH', public_path('spa.html'));
    }
)->where('path', '.*')
 // Redirect to Nuxt from within Laravel
 // by using Laravels route helper
 // e.g.: `route('nuxt', ['path' => '/<nuxtPath>'])`
 ->name('nuxt');

Forward multiple specific routes to nuxt (using laravel-nuxt)

This example assumes option nuxtConfig.router.base to have been set to '/app/'

❗❗❗ Attention ❗❗❗:
Nuxt router has problems resolving the root route without a trailing slash.
You will have to handle this in your server configuration:

  • Nginx: rewrite ^/app$ /app/ last;

  • Apache: RewriteRule ^/app$ /app/ [L]

  • Artisan: In server file:

    // ...
    
    // This file allows us to emulate Apache's "mod_rewrite" functionality from the
    // built-in PHP web server. This provides a convenient way to test a Laravel
    // application without having installed a "real" web server software here.
    if ('/app' === $uri) {
        header('Status: 301 Moved Permanently', false, 301);
        header('Location: '.$uri.'/');
    
        return true;
    } elseif ('/' !== $uri && file_exists(__DIR__.'/public'.$uri)) {
        return false;
    }
    
    // ...

config/nuxt.php:

return [
    'routing' => false,
    'prefix'  => 'app'
    'source'  => env('NUXT_OUTPUT_PATH', public_path('app/index.html'))
];

routes/web.php:


use M2S\LaravelNuxt\Facades\Nuxt;
/**
 * Forward specific route to nuxt router
 *
 * This route is redered by `<nuxtRoot>/pages/index.vue`
 */
Nuxt::route('/')->name('nuxt');

/**
 * Forward all paths under a specific URI to nuxt router
 *
 * These routes are rendered by:
 * - if `{path} = '/'`
 *   `<nuxtRoot>/pages/subpage.vue`
 *    or
 *   `<nuxtRoot>/pages/subpage/index.vue`
 *
 * - if `{path} = '/<path>'` (`<path>` may contain slashes '/')
 *   `<nuxtRoot>/pages/subpage/<path>.vue`
 *   or
 *   `<nuxtRoot>/pages/subpage/<path>/index.vue`
 */
Nuxt::route('subpage/{path?}')->where('path', '.*')
 // Redirect to a spcific subpage/<path> from within Laravel
 // by using Laravels route helper
 // e.g.: `route('nuxt.subpage', ['path' => '/<path>'])`
 ->name('nuxt.subpage');