GithubHelp home page GithubHelp logo

Comments (7)

ankane avatar ankane commented on July 20, 2024

Hey @kohheepeace, thanks for following up with the solution 👍

from ahoy.js.

firdausmaarof avatar firdausmaarof commented on July 20, 2024

@kohheepeace you are a savior. Thanks.

from ahoy.js.

montekaka avatar montekaka commented on July 20, 2024

@kohheepeace followed the next.js config,
in utils/ahoy.js

// https://github.com/ankane/ahoy.js/issues/35
import Cookie from 'js-cookie'

const baseURL = process.env.RAILS_ENDPOINT;

const isServer = (typeof window === 'undefined')
let ahoy

if (!isServer) {
  ahoy = require('ahoy.js').default;

  ahoy.configure({
    urlPrefix: '',
    visitsUrl: `${baseURL}/ahoy/visits`,
    eventsUrl: `${baseURL}/ahoy/events`,
    page: "Bio page",
    platform: 'Web',
    useBeacon: true,
    startOnReady: true,
    trackVisits: true,
    cookies: false,
    cookieDomain: null,
    headers: {'Authorization': `Bearer ${Cookie.get('accessToken')}`},
    visitParams: {},
    withCredentials: true
  })
}


export default ahoy

but keep getting

Access to XMLHttpRequest at 'http://localhost:3000/ahoy/events' from origin 'http://localhost:5000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

in rails ahoy.rb

class Ahoy::Store < Ahoy::DatabaseStore
end

# set to true for JavaScript tracking
Ahoy.api = true

# set to true for geocoding
# we recommend configuring local geocoding first
# see https://github.com/ankane/ahoy#geocoding
Ahoy.geocode = true
Ahoy.job_queue = :low_priority

# since the request is from next JS server
# FIXME
Ahoy.track_bots = true

Please advise.
Thanks!

from ahoy.js.

kohheepeace avatar kohheepeace commented on July 20, 2024

@montekaka CORS error is very common issues. Using the rack-cors gem may solve the problem. And this is a tutorial https://admatbandara.medium.com/setting-up-cors-to-my-rails-api-a6184e461a0f to add this gem.

from ahoy.js.

montekaka avatar montekaka commented on July 20, 2024

@montekaka CORS error is very common issues. Using the rack-cors gem may solve the problem.

Thanks so much for the quick response. I setup it as well. I have cors.rb:

Rails.application.config.middleware.insert_before 0, Rack::Cors do
  allow do
    origins '*'
    resource '*',
    headers: :any,
    expose: ['access-token', 'expiry', 'token-type', 'uid', 'client'],
    methods: [:get, :post, :put, :patch, :delete, :options, :head]
  end
end

from ahoy.js.

montekaka avatar montekaka commented on July 20, 2024

I might have my issue solved by change the withCredentials to false.

from ahoy.js.

fkapsahili avatar fkapsahili commented on July 20, 2024

I actually had to add credentials: true to the cors.rb initializer:

resource "*",
   headers: :any,
   methods: :any,
   credentials: true

from ahoy.js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.