How can you deactivate the vue-router for specific routes? Is it possible to operate a single-page application backend while utilizing a non-single-page

I'm currently working on a website/webapp using Laravel 5.3 and Vue 2. Since SEO is crucial, I've decided to keep the frontend/crawlable part of the site in Laravel + Blade, with only minimal sections in Vue 2.0. This way, I can avoid using Ajax to load page content and allow search engine crawlers like Google to easily index the site (although Google may not wait for Ajax to fully load).

On the backend, however, I plan to go full SPA with Vue and VueRouter.

The challenge now is how best to separate these two components?

I intend to make my backend accessible via /manager

So far, here's what I've come up with:

# routes.php

### Laravel/Frontend routes go here (before SPA) ###

Route::get('/manager/{spaPlage?}', ['middleware' => 'auth', function () {
    return view('manager.index');
}])->where(['spaPlage' => '.*'])->name('manager.index');

And then in Vue, I have:

const routes = [
    { path: '/', name: 'dashboard.index', component: require('./pages/manager/Dashboard.vue') },
    { path: '/categories', name: 'categories.index', component: require('./pages/manager/categories/Index.vue') },
    { path: '/category/:id', name: 'category', component: require('./pages/manager/categories/Category.vue') }
];

const router = new VueRouter({
    routes,
    base: '/manager'
})

const app = new Vue({
    el: '#app',
    router
    ...

This setup seems to be functional, but it doesn't quite feel right as the Vue Router still affects the frontend by appending hash/hashbang. Is there a more effective way to separate the Laravel frontend from the Vue SPA backend?

Answer №1

To clear up any misunderstandings for future reference:

It is possible for Google to crawl JavaScript-based front end websites without the need for pre-rendering or server-side rendering. However, Google uses Chrome 41 to crawl and render the site, so ensure your JavaScript is polyfilled enough to support Chrome 41 features. I have personally had success using Babel for this purpose.

Using Laravel for the frontend with my Vue SPA backend

This approach is incorrect. If you are considering this setup, it's advisable not to proceed with it. Laravel should serve as both your backend and frontend (utilizing blade + bootstrap + Vue/react), or solely as your backend. Vue.js is designed for frontend tasks and should not be used as a "backend." Leave all database queries, authentication, calculations, emailing, etc. to be handled by Laravel. It's perfectly feasible to develop an entire API with Laravel and incorporate Vue exclusively for the frontend. While it might seem possible to make it work through hacks, it's best to avoid unnecessary complications.

However, there are scenarios where only certain parts of a website operate as a Single Page Application (SPA). For instance, if a blog section is separated from other static pages like "about us" or "contact us," you could gradually update different sections of the site over time. In such cases, specific routes can be designated for Vue Router, while Laravel manages the rest.

Route::get('/DIR/{vue_capture?}', function () {
    return view('vue.index');
})->where('vue_capture', '[\/\w\.-]*');

By implementing the above code snippet, Vue Router can handle operations within the 'DIR' subdirectory, leaving Laravel to manage other routes concurrently.

If your application requires Vue Router to operate at the root domain, simply place it at the end of your routes file. This ensures that Vue Router acts as a "catch all" for routes not specified before it, similar to an early return statement in programming.

Answer №2

If you happen to stumble upon this, I couldn't find any information on how to deactivate VueRouter for specific links. It seems like it's not feasible and probably not worth the effort.

As a result, I've made the decision to overhaul my entire codebase to function as a single-page application (SPA). It's long overdue and there's no valid reason not to do so. To optimize for SEO and other purposes, I'm utilizing Prerender.io's self-hosted service for pre-rendering content.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Execute a function upon pressing the enter key

Currently, I am working on coding a webpage with a form that includes one field where users input a set of numbers. After typing in the numbers, they should then press a button labeled 'Run' to execute the code. However, the issue arises when use ...

HTML filtering using the <select> element

Is there a way to implement this script for options instead of buttons? The goal is to filter the HTML section, but it seems to not work with <option> tags. Instead of displaying the all class, it shows nothing. CSS .show { display: block; } .filt ...

Having trouble displaying multiple image sliders per page using vue.js when pulling images from a local folder

I'm struggling to display multiple images in a slider on each page using vuetify and Vue-awesome-swiper but I can't seem to get it to work. Can someone please assist me with this? <v-sheet class="mx-auto" elevation="8" ...

What is the process for building a JSON-formatted dictionary?

My intention is to structure my data in a way that can be accessed using a specific key. The current arrangement looks like this: const dict = []; dict.push({"student": "Brian", "id":"01", "grade":& ...

UI experiencing issues with selecting radio buttons

When trying to select a radio button, I am facing an issue where they are not appearing on the UI. Although the buttons can be clicked, triggering a function on click, the selected option is not displayed visually. <div data-ng-repeat="flagInfo in avai ...

How can you showcase the content of an object within an array?

I am currently working on a project component that involves retrieving and logging data. However, I am facing an issue with accessing objects within my array of clients to display them properly in my table. Here is the script I am using: <script> ex ...

Is there a problem with this spell checking feature implemented with jQuery?

<script type="text/javascript"> // Validate the spelling in a textarea $("#check-textarea").click(function (e) { e.preventDefault(); $(".loading").show(); $("#text-content") .spellChecke ...

Show the list in a circular buffer fashion

I am working on a project that involves creating a unique UI element. In Frame #2 of my professionally designed diagram, I envision a list that functions as a ring buffer/rolodex when it exceeds four items. The list would scroll in a loop with the top and ...

Using jQuery DataTable to fetch JSON data upon Document Loaded

I am struggling to implement a PHP script that returns JSON data to populate a DataTable. The PHP script 'api/exceptions_all.php' is as follows: <?php $select = "SELECT '', [FOR_PARTNER], [FOR_NAME] FROM [brokerage].[dbo].[for_ex ...

The alert box for model validation summary errors is deactivated when hidden using .hide() method

In my MVC web application form, there is an optional postcode finder. If there is no match for the entered postcode, I add a custom error message to the .validation-summary-errors section. After the error is fixed, I remove all instances of the postcode cl ...

Unable to load the node modules

In my development journey, I created an ASP.NET MVC project using Angular 2 in Visual Studio 2017 and set up node for package management. Here is a snippet from the package.json file: { "version": "1.0.0", "name": "asp.net", "private": true, ... ...

Mongoose fails to carry out internal query using Promise mechanism

Just diving into the asynchronous and await world, I decided to play around with Mongoose + MongoDB + Node.JS. Here is a snippet of my code: exports.updateBrandPreferences = async (req,res) => { var userID = req.body.playerID; var newBrands = r ...

Implement AJAX and PHP to submit form data along with the page ID and session information

I am currently developing a school book library website project that allows students to browse and select books, as well as send requests to the librarian to borrow them. However, I am still learning jQuery and facing some challenges in handling issues. H ...

Issue with Intel XDK: the document.location.href is directing to an incorrect page

Hello Community of Developers, I have been experimenting with different methods but still haven't found a solution. In my project using Intel XDK, whenever I attempt to change the page using location.location.href = "#EndGame" or similar codes in Java ...

Is there a way to transform my json data into a particular structure?

My json string looks like this: arguments[0] = [{"one":"1","two":"1","three":"15","four":"esj","five":null,"six":"2015-10-15 00:00:00","seven":null}, {"one":"1","two":"1","three":"15","four":"esj","five":null,"six":"2015-10-15 00:00:00","seven":null}, {"o ...

Delete the final element from the json object

I need to extract the data from a JSON object and exclude the last element called 'week2'. Here is the object with properties like username, Geo, status, month, and week2: const response = [{ Username: "Denisse Morales", Geo: "NSU", ...

Assistance needed with dynamically resizing a background image

Is there a way to automatically adjust the size of a background image for an element? For instance, I want my links in HTML to have a background image with slanted borders and rounded corners. Usually, you would set the width of the anchor element to fit t ...

Vue3 - Utilizing a method to dynamically alter an input property

Currently, I am in the process of developing a Vue application that incorporates a map feature. The main functionality involves determining whether a given position on the map is over water or land. If the position is over water, I want to iterate through ...

Transform the angular code in order to execute innerHTML functions

function campform() { $.ajax({url: "{{ path('campform') }}", success: function(result){ $("#respuesta").html(result); }}); } I am having trouble with the angular code in the result I received. Can anyone provide guidance on how t ...

No reply received on the web browser

I have written a code that is intended to read a JSON file and display it in the browser. The code is functioning correctly as it prints the data to the console, but for some reason, the data is not displaying on the browser. app.post("/uploadfile&q ...