Global Inertia Headers

How can I ensure that a custom header (Accept-Content-Language) is sent with every request, including Inertia manual visits?

Below is the code snippet where I define and set the header:

import axios from 'axios';

const lang = localStorage.getItem('vue_i18n_locale');

if (lang) {
  axios.defaults.headers.common['Accept-Content-Language'] = lang;
  axios.defaults.headers.common['Accept-Language'] = lang;
}

window.axios = axios;

When trying to make a call like this:

$inertia.post('some-route', { some: data })

I am aware of manually passing the header prop during the visit, but I want this header to be automatically included in all requests throughout the application without needing to add it manually each time.

Is there a way to achieve this auto-inclusion? My searches online have not yielded a solution, so I'm hoping someone here might have some insights.

Answer №1

Hopefully, this information proves to be useful for you.

import axios from 'axios';
import { InertiaApp } from '@inertiajs/inertia-vue3';

axios.interceptors.request.use((config) => {
  const lang = localStorage.getItem('vue_i18n_locale');
  if (lang) {
    config.headers['Accept-Content-Language'] = lang;
    config.headers['Accept-Language'] = lang;
  }
  return config;
});

const app = document.getElementById('app');

new InertiaApp({
  resolve: (name) => import(`./Pages/${name}.vue`),
  setup({ el, App, props, plugin }) {
    return createApp({ render: () => h(App, props) })
      .use(plugin)
      .mount(el);
  },
  page: JSON.parse(app.dataset.page),
});

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

How to generate a list in Vue.js using an array

I am facing an unusual issue while trying to create a list using Vuejs. Here is the structure of my array: const sr = new Vue({ el: '#singlerecipe-app', data: { checkeditems: [], }, }); This array is initialized in my HTML as follows: ...

Is your Jquery code behaving sporadically, functioning for a moment and then ceasing to

After successfully implementing a slide mechanism on a page, it suddenly stopped functioning properly without any apparent changes being made. Here is the code snippet: $(document).ready(function () { var hash = window.location.hash.substr(1); var ...

unable to transfer Vuex store information to the graph

Currently, I am facing an issue with passing my vuex store data into my apexcharts graph. Despite my efforts, it seems like the data is not being displayed correctly. Can anyone provide guidance on what I might be doing wrong? My main objective is to updat ...

Show a collection of files in a table format

Here is the current code I have: index.html <!DOCTYPE html> <html> ... </form> </div> </body> </html> And here is my code.gs function doGet() { return HtmlService.createHtmlOutputFromFile('index'); } ...

Using a SharedModule in Angular2: A Guide

I have a single Angular2 component that I need to utilize across multiple modules. To achieve this, I created a SharedModule as shown below: import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-bro ...

Error: The function expressJwt is not recognized as a valid middleware

As I delve into learning about middlewares, I encountered an issue when trying to import express-jwt. The syntax I used was: const expressJwt = require('express-jwt') To address the problem, I uninstalled the current version of express-jwt and i ...

Tips for importing an external Vue script into your Vue.js application

Being new to Vue.js, I have a question. I have written a Vue script (Methods) that I would like to use in other components of my App. I have placed this code within a component between two script tags, but I am unsure of how to utilize the functions from t ...

developing a multimedia player using HTML5

I'm attempting to create a dynamic video "collage" that showcases videos with different aspect ratios in a flexible grid layout. Each row should have videos of the same height, while filling up the horizontal space within a container. With known widt ...

jQuery does not function properly when used with string variables

Why am I experiencing different results in Google Chrome when using a hard-coded string versus storing the same string in a variable? While the hard-coded string works properly, the same string stored in a variable does not produce the expected outcome. ...

Compare the precise value of $(window).height() to a specific scroll value

Initially, I retrieve $(window).height() and compare this height with the specific scroll value. $(window).scroll(function (event) { var scroll = $(window).scrollTop(); var windowHeight = $(window).height(); console.log("window hei ...

Which delivers better performance: HTTP request from server or client?

Considering the optimal performance, is there a difference in using Angular's HTTP request within a MEAN-stack environment compared to utilizing Request.js or similar for server requests? ...

Pull the data from jQuery/JavaScript/AJAX and store it in the database using ASP.NET/C#

I am working on creating a form that includes textboxes and a five star rating feature. The goal is to save the data entered in the fields into a database upon submitting. While implementing the textboxes was straightforward, I am facing challenges with e ...

Material-UI React Native components: Injecting Styles without Props

import {createStyles, WithStyles} from "@material-ui/core"; const styles = (theme: Theme) => createStyles({ root: {} }); interface MyProps extends WithStyles<typeof styles> { } export class MyComponent extends Component<MyProps ...

The submenu malfunctioned, resulting in it displaying only text

Recently, I encountered an issue with a submenu in my Bootstrap project. The submenu appears as text instead of a button to navigate to another page. Below is the code snippet I am using: This problem occurs with the combination of Vue.js and Bootstrap 4. ...

I'm puzzled as to why, but my code seems to be causing an issue with the command prompt, possibly due

Recently, I've been taking on the challenges of Advent of Code. Day 2 has me stumped, as there's a peculiar issue with the code when running it through node.js in the command prompt: const fs = require("fs"); var valid = fs.readFileSync("inpu ...

What are the best strategies for breaking down an AngularJS application into smaller modules and managing routing effectively?

What is the most effective way to divide an AngularJS application into smaller modules? For instance, if I have a blog post with commenting functionality, I could potentially separate them into modules like "posts" and "comments", rather than having all th ...

Explore the search bar with functional filtering for JSON items

I'm currently working on creating a dynamic filter bar integrated with a search functionality. My data is stored in JSON format, including details such as artist name, title, source, and more. Despite successfully retrieving results through console.lo ...

We were unable to locate a declaration file for the module known as 'firebase-tools'

As I delve into writing my inaugural cloud function for Firebase, I find myself in need of the firebase-tools module. To bring it on board, I updated my dependencies by editing the package.json file and executing the command npm install. Next, I attempted ...

Steps to duplicate the package.json to the dist or build directory while executing the TypeScript compiler (tsc)

I am facing an issue with my TypeScript React component and the package.json file when transpiling it to es5 using tsc. The package.json file does not get copied automatically to the dist folder, as shown in the provided screenshot. I had to manually copy ...

Ways to change a value into int8, int16, int32, uint8, uint16, or uint32

In TypeScript, the number variable is floating point by default. However, there are situations where it's necessary to restrict the variable to a specific size or type similar to other programming languages. For instance, types like int8, int16, int32 ...