spatie / laravel-server-side-rendering

Server side rendering JavaScript in your Laravel application
https://sebastiandedeyne.com/posts/2018/server-side-rendering-javascript-from-php
MIT License
662 stars 63 forks source link

How can I use axios? A get the error TypeError: adapter is not a function at dispatchRequest. #53

Closed vtoropchin closed 4 years ago

vtoropchin commented 4 years ago

When sending a get request, the promise is redirected with an error:

TypeError: adapter is not a function at dispatchRequest (/Users/vladimir/Programming/GR/lawsystem.test/storage/app/ssr/28d918e4632a3b48ddcaf89db6812bd7.js:1058:10) TypeError: adapter is not a function at dispatchRequest (/Users/vladimir/Programming/GR/lawsystem.test/storage/app/ssr/28d918e4632a3b48ddcaf89db6812bd7.js:1058:10) at async Promise.all (index 0) "

webpack:

const mix = require('laravel-mix');

mix.js('resources/js/CustomerPortal/app-server.js', 'public/js/customer-portal');
mix.webpackConfig({
    target: 'node',
});

Here I make get request.

getServices() {
      return axios.get('https://lawsystem.grani-riska.ru/api/services/get',
      ).then(response => {
          return response;
      }).catch(error => {
          console.log(error); //here I catch the error
      })
  },

app.js

import Vue from 'vue';
import store from '../Core/store';
import VueRouter from 'vue-router';

const router = new VueRouter({
    mode: 'history',
    routes: routes.concat(serviceRoutes.routes)
});

const app = new Vue({
    render: h => h(AppComponent),
    router,
    store
});

export default {app, router, store}

app-server.js

import app from './app'
import renderVueComponentToString from 'vue-server-renderer/basic';

new Promise((resolve, reject) => {
    app.router.push(context.url);
    app.router.onReady(() => {
        const matchedComponents = app.router.getMatchedComponents();
        if (!matchedComponents.length) {
            return reject(app.app);
        }

        context.rendered = () => {
            context.state = app.store.state
        };

        resolve(app.app);
    }, reject);
}).then(app => {
    renderVueComponentToString(app, (err, html) => {
        if (err) {
            throw new Error(err);
        }

        dispatch(html);
    });
}).catch((err) => {
    throw new Error(err);
});

Version axios - 0.19.2 vue - 2.6.11 laravel-server-side-rendering = 1.3

kieuminhcanh commented 4 years ago

I found that context.rendered not work

JonathanMurre commented 4 years ago

I have the same problem, how did you fix this?

nicovigil1 commented 4 years ago

samesies

SerhiyP commented 3 years ago

Hello I suppose that problem there because the process variable is empty when script runs

suhostv commented 3 years ago

@SerhiyP what process variable are you talking about? I have the same problem during ssr, on client-side requests are fine

SerhiyP commented 3 years ago

I don't remember what was the problem

But you can try to set in webpack target: 'node'

It might help you

suhostv commented 3 years ago

@SerhiyP thanks for your response there is the reason why we don't have this in our webpack configuration, it breaks our logger ('fs' module issues) can you explain why target:node would fix axios issue? not really getting this...

SerhiyP commented 3 years ago

Oh... Sorry For adapter, you should use

`import adapter from 'axios/lib/adapters/http'

axios(URL, { adapter: adapter }) `

suhostv commented 3 years ago

@SerhiyP tried this, getting XMLHttpRequest is not defined error when doing that same as in this discussion: https://github.com/axios/axios/issues/2968

yasen002 commented 2 years ago

I don't know why this issue is closed.... I am getting the same error. one way to work around this, I guess is to use serversideProps in each individual page.

dmwin72015 commented 2 years ago

same error,how to fix it???

aadarshp31 commented 2 years ago

Oh... Sorry For adapter, you should use

`import adapter from 'axios/lib/adapters/http'

axios(URL, { adapter: adapter }) `

This answer worked for me perfectly fine and is definitely a lifesaver so Thanks for your contribution @SerhiyP . I'm grateful ❤️

MY USE CASE I am working on a vscode web extension which I want to work in desktop (nodejs) environment. Axios was throwing the exact same error for me. This fix helped me resolve this issue.