How To Add Nexts.js to Laravel

10,296

Adding a strong and sophisticated framework such as Next.js to laravel, would be very long long way to code and debug, and IMHO wouldn't be the optimal or feasible solution, specially if we came to SSR

Instead, you can use it as if you are making an SPA in react, which use Laravel as api or use Lumen; and then pass the data to Next.js through ajax, while using Next router.

To do this, there is a routing gotcha you need to make sure you don't fall in its trap, which is server routing and Next routing; you just pass this to Laravel route/web.php

Route::fallback(function () {
    return view('your-react-page');
});

passing this will make sure that all routes are falling back to this page, if you want any other links to be served by Laravel, just make sure to add them before the fallback.

or if you want to specify a route, you can always use regex.

Share:
10,296
Mehdi Yaghoubi
Author by

Mehdi Yaghoubi

Hello I am Mehdi Yacobi, a FullStack Laravel developer. I design and develop any website and web app from the ground to deployment. I use scss, tailwind or bootstrap, Ajax, JavaScript, jQuery, and other technologies for development. I can handle authentication, notification, SMS, payment, complex database relations, REST APIs and HTTP requests, RTL and LTR language options, and other features. You can trust because I am secure, reliable, and on time. [email protected] 989122380343

Updated on August 25, 2022

Comments

  • Mehdi Yaghoubi
    Mehdi Yaghoubi over 1 year

    I want to add next.js framework to Laravel In preset we have [php artisan preset react] But I'm not sure how to do with next.js

    How can I add next.js to laravel clean and correct as adding React with preset?

  • Deep Sojitra
    Deep Sojitra over 2 years
    So after all is that possible to create app with next js and Laravel or not?
  • Hamza Mohamed
    Hamza Mohamed over 2 years
    @DeepSojitra yes, you can using Laravel as backend and NextJs as SPA, the question is about adding NextJs as a preset.