How do I ask wordpress to serve my react app? - wordpress

I am working for an organization that has a wordpress website, I am building a react app that will pull data from the wordpress database through the wordpress API. I am trying to figure out how I can tell wordpress to serve the react app as though the user were simple visiting another page on our site. Is this possible or is my only option to serve the react app on another server?

The first thing to know about React on a standard server : you will be able to serve your react app separately onto the same server only if you have access to the CLI (command-line). React has to be built by webpack so it'll be impossible to compile, build and serve your react app without using npm or yarn.
If you don't have access to the CLI, and the Gutenberg wordpress editor is available inside your WordPress, I do suggest you to enqueue React into your theme/custom theme or inside a plugin. A link worth 100 words, please take a look at https://javascriptforwp.com/enqueue-react-in-wordpress/ in order to do it properly.
Basically, you will just have to set an ID or a class name to an element of your DOM body, then React will be mounted on this block and you'll use the ReactDOM render() method from wp.element JS object to do so (React lib is available and given from the Gutenberg editor 😊)
Hope this can help you with your question 😇

I figured it out. Thank you for your suggestions. I have decided to create a sub directory in my public_html folder and put the react app there.

Related

If a website is constantly changing, can it be implemented with next.js?

I hope you are doing well
I want to make a book site with next.js. My site needs to be updated and add products every day. I want to know now if it is appropriate to make this site with next.js?
Now, my website is complete and I ran it with next.js, my website is with node.js, and now every product that I add, I have to go and get a build from the next.js project and upload it to the host, and is this a difficult job? If anyone has a better way, I would be grateful if they could help
Look at this Showcase on next.js official website. It's powerful enough to make any kind of dynamic website. You need to use getServerSideProps with the database of your choice. If you are using Static HTML Export you have to rebuild and upload it again whenever you make any changes.
There's also a lot of options to automate next.js deployment, Best option for your use case is to use services like Digital Ocean App Platform so whenever you push some changes to git repo it automatically rebuild and update your site with fresh content.

Wordpress & Vue & Nuxt - Integration - wp_enqueue_script

I have an existing website where I am gradually replacing the html/css interfaces with Vue technology. So now I have pages with two logics
Pages completely in vue
Pages with html/css parts and vue parts.
Currently I am using Vue (without Nuxt) with success to do this migration. In my wordpress theme I load vue scripts as follows
wp_register_script('vue-app', $directory_uri . '/js/app.js', array(), $version, true);
wp_enqueue_script('vue-app');
In order to improve my project I am switching to Nuxt. But I can't keep the same logic of integration. Nuxt does not create a unique app.js. Nuxt is generating a whole bunch of files and I cannot load them synchronously. Neither in build nor in dev (hot-module-replacement).
I don't know how to keep my logic 1 and 2 with Nuxt. Do you have any solutions ?
If you want to use Nuxt to it's full potential and keep Wordpress, you need to use Wordpress as a headless CMS. Meaning that you may use Full static and generate all of your pages during build time by reaching to your Wordpress app.
I guess that your current Wordpress + Vue flow is lacking SEO hence why you're trying to reach upon Nuxt. But to my knowledge, you will not be able to just plug it like that with your wp_register_script.
Or maybe try to mount it for every one of your routes .../about, .../our-team etc... Not a wordpress expert tho.

How to use Nuxt.js components in a WordPress project?

I have a large Nuxt.js project with many different components. Now I want to reuse these components in a new project which uses WordPress. So for example I want to be able to display a certain component on a WordPress page or post.
My first thought was to only use Vue.js without Nuxt but then I would not be able to use the Nuxt components without having to remove Nuxt-specific code. I know that there is also a way to use Nuxt programmatically as a middleware but I can't figure out how to do this with WordPress.
Can someone guide me into the right direction?

Can I add anuglar app to my wordpress site?

So i have built an angular website that uses the GoogleBooksAPI to fetch some book list. And then I show it on my single page app.
Now, i'd like to deploy it somehow to see if it's working.
So i tried to add the project to github pages. github project link:
https://github.com/DrorSC/DrorSC.github.io
And was unsuccesful..
So i thought on a different option. I have a domain, and a wordpress site on it. Maybe i can add the files to my Cpanel and load it into my domain? Or can i do it straight from wordpress?
Tried to look for answers, couldn't find.
Please help. thank you :)
If I understand correctly I think Angular Element will be your best choice. It works the best if you want to integrate Angular web app into your existing website.
Building Custom Elements / Web Components with Angular 6
After build the project. You can use the content of the dist directory into your host.
If you will put your code into a subfolder, you will need specify the path as argument on the CLI for the build.

Mixing Wordpress eCommerce site with external login based Vue Frontend Best Practices

I am new to web development and and would like advice about what is the usual best practices to achieve what I am trying to do.
I am in charge of writing a frontend app using Vue. This frontend will require a login to access the app. The app will be hosted on Amazon.
However we are also wanting to build an eCommerce site in Wordpress or equivalent that sits on the root of the website, (eg www.mywebsite.com) with the app accessible through a "sign in" button. If the user presses the sign in button we want the location bar location to not change from the website they are on (www.mywebsite.com), even though the Wordpress site and Vue frontend may be stored on different locations/domains.
If the site is made in Wordpress, we can get another developer to do that webpage for us, while I work on the frontend. However we want the experience to be relatively seamless between connecting to the wordpress root site, clicking on the sign in button and being taken to the Vue frontend.
Is this possible? Do both sites have to be hosted on the same server for it to work (eg an Amazon EC2 server which hosts two webpages, the wordpress one and the vue one)?
Or can the wordpress put a link to the Vue frontend which doesn't change the web location to the user?
Or (worst case scenario) do we need to make both webpages in Vue as one Vue application, some pages requiring login to access and others not?
Thanks kindly for any assistance.
So in order to use Vue and WordPress together, you can either write 2 separate applications. One for the Frontend and one for the Backend, which is perfectly fine or you can simply write a WordPress Application with a Vue Theme.
This is how this could look like:
WordPress Backend
You can use a normal WordPress backend, where you can build the architecture you want to. In addition, you need to set up the REST-API Wordpress provides. So this also means, just use plugins which support it. Otherwise, you would need to write custom endpoints. For example, Woocommerce would be a good e-commerce plugin, since it supports the API.
Learn more about it here: https://v2.wp-api.org/
Of course, you can host your WordPress installation on Amazon. Here is a good guide for that: https://aws.amazon.com/de/getting-started/tutorials/launch-a-wordpress-website/
Notice: The REST-API is available in core since 4.7. If you are using an older version you would need to install a plugin.
WordPress Frontend
In order to create your Frontend, I'd recommend you to make a custom theme. Since you are just serving static files this can be done pretty easy. But still, you don't need to reinvent the wheel.
There's a really good starter theme out there: https://github.com/EvanAgee/vuejs-wordpress-theme-starter
It comes with all the stuff you need to get started and since it is just a theme, you can simply set up a usual WordPress hosting without handling Domains of multiple applications.
In order to get the data from the Backend, you would simply make calls to the REST-API. Here's an example: https://github.com/EvanAgee/vuejs-wordpress-theme-starter/blob/master/src/api/index.js

Resources