How to make a React.js-based page within Discourse that only re-uses its header and session?

AI 生成的摘要

meglio seeks to create a custom page at a specific Discourse URL (e.g., /my-page) that displays the native Discourse header and footer but renders custom content using React.js instead of the default Ember.js framework. The primary goal is to avoid writing a full custom plugin while retaining access to the Discourse JavaScript environment and the streaming API.

meglio initially asks if this can be achieved solely via custom JS scripts in the admin panel. However, Falco clarifies that this is not possible without a custom plugin because both the Rails and Ember routers must be explicitly aware of the new route to handle direct URL access correctly. meglio explores workarounds, such as overriding templates for specific topics or intercepting 404 errors, but acknowledges the complexity of server-side rendering versus client-side interception.

gdpelican outlines the difficult “circus” required to make a non-plugin approach work: relying on 404 interception, injecting React components into the DOM, and accessing internal containers (like messageBus) via fragile hacks. gdpelican questions why a plugin or Ember is not an option, noting the approach is inefficient. meglio explains that while they are not opposed to learning, they strongly dislike the Ember paradigm and prefer leveraging their existing React expertise. The discussion concludes with meglio reiterating the desire for a simpler method to embed React components into Discourse pages without deep framework integration.

I would like to create a page under the Discourse’s main domain, for example:

https://meta.discourse.org/my-page

I’d like this page to get full access to Discourse js environment, but only show the main header:

Under the header, I’d like a React container with my own custom JS code.

Question:

  1. How can I do it without making a custom plugin for Discourse, but by only providing custom JS scripts in admin panel?
  2. I’ll need to still keep the Discourse header live
  3. Will I be able to subscribe to Discourse streaming API without using Ember?

As you can understand, the whole goal for me here is to make a custom page with the native Discourse header, but with using React.js and not touching Ember.js at all.

Any advice appreciated!

This isn’t possible, the router (both rails and ember one) must be aware of that route.

The ember router is JS, so I thought what if there is a way to add a route to it AND provide a template as well - such a template which would not make Ember request anything from the server, but just load react JS library and some custom JS files with React components?

But if the user go direct to the /my-page you’re toasted.

Right…
What if a topic is created for such a page, and then a template is overriden for this topic’s URL in Ember?

I just looked at what HTML is returned per topic when it’s accessed directly by opening its URL. So, all the HTML gets populated straight away:

  • Header
  • Title
  • Breadcrumb
  • Posts
  • Footer

This means the template has been processed on the server.

Is there a way to subscribe to an event when a new topic is opened in Discourse, including both navigation and opening by direct URL?


Now, if I make a separate page: https://meta.discourse.org/my-page.html

Will it be possible to load the Discourse’s header section into that page and still have access to all the Discourse environment and streaming API from withing JS?

What you’re proposing would have to go through some circus like

  • Call to domain/my-page.html
  • Discourse 404’s
  • Your custom JS catches that 404, recognizes that it came from your custom route somehow, and.. renders the whole template into the 404 div?
  • Meanwhile, it also loads a react component, which.. I guess gets slid into a particular DOM element using react-dom-inject or something?
  • Then it subscribes to messageBus, err maybe by loading something like let messageBus = Discourse.__container__.lookup('messageBus:main').subscribe? (<-- this code I think still works today, but likely won’t forever, or even for much longer)

I don’t quite get it; why is creating a custom plugin, or using Ember, not an option here?

I get that the plugin architecture and perhaps Ember has a steep learning curve, but this is really using a :hammer: to churn the butter.

Because I’m not a R programmer, nor am I an Ember programmer. In the same time, I have lots of React.js experience and I love building stuff with using it.

It’s not that learning Ember is difficult. It’s that I hate its paradigm, so would like to keep its usage to the minimum.

Okay, well, thanks for commenting - anyway, this one is not the critical point. What I wanted is to allow easy React embedding in particular Discourse pages.