Last active
June 8, 2021 07:54
-
-
Save 1isten/6d01e70e52f6d04a6438a191cb2b0646 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
lite version |
💡remember to edit .gitignore
the for Laravel project to ignore compiled files
+ /public/assets
+ /resources/views/frontend.blade.php
Nuxt inside Laravel
basically, generate nuxt static dist
into laravel and serve the dist
like another public
folder.
// frontend/package.json
{
"scripts": {
// ...
"pregenerate:laravel": "rm -rf node_modules/.cache .nuxt dist ../backend/dist",
"generate:laravel": "BASE_URL=/ BASE_DIR=/prefix/ PUBLIC_PATH=/ nuxt generate",
"postgenerate:laravel": "mv -fv dist ../backend/dist"
}
}
// frontend/nuxt.config.js
export default {
// ...
axios: {
baseURL: process.env.BASE_URL || '/',
},
router: {
base: process.env.BASE_DIR || '/',
},
build: {
publicPath: process.env.PUBLIC_PATH || '/_nuxt/',
},
generate: {
fallback: true,
},
// ...
};
// backend/routes/web.php
// ...
Route::get('/', 'HomeController@serveStatic');
Route::fallback('HomeController@serveStatic');
// backend/app/Http/Controllers/HomeController.php
use Illuminate\Http\Testing\MimeType;
// ...
class HomeController extends Controller
{
public function serveStatic(Request $request)
{
$base = 'prefix'; // or ''
$dist = 'dist';
$path = base_path(($base ? $dist : "$dist/") . str_replace($base, '', $request->path()));
$path = is_dir($path) ? "$path/index.html" : $path;
return file_exists($path) ? response()->file($path, ['Content-Type' => MimeType::from($path)]) : abort(404);
}
}
may reference: https://github.com/cretueusebiu/laravel-nuxt
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
if
history
mode is enabled, edit thebase
option inrouter.js
according to the previous Laravel route path settingsprocess.env.BASE_URL
corresponds to thepublicPath
option invue.config.js
, but we don't use this value here because we won't modify thepublicPath
, instead we leave it with default value'/'
. Because if we setpublicPath
to'/frontend/'
Laravel would not able to serve the route when usingphp artisan serve
and add a universal fallback in
web.php